【问题标题】:Android share image on snapchatAndroid 在 snapchat 上分享图片
【发布时间】:2016-05-29 08:18:11
【问题描述】:

我正在使用此代码来分享分数的屏幕截图:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/png");
        Uri image = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/sharescore.png");
        try {
            // create bitmap screen capture
            View v1 = v.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(image.getPath());

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
        sharingIntent.putExtra(Intent.EXTRA_STREAM, image);
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));

当我使用“imgae/jpeg”类型时,它适用于 Whatsapp Twitter 等,但不适用于 snapchat 和 Instagram。它导致错误“所选图像无法打开”。我怎样才能使这项工作适用于 snapchat 和 instagram。

【问题讨论】:

    标签: android android-intent share instagram snapchat


    【解决方案1】:

    他们的问题是,我将屏幕截图保存到 sd 卡,但 snapchat 没有从外部存储读取文件的权限。我必须使用只能由我的应用程序访问的应用程序目录,并且必须使用我自己的文件提供程序(com.test.fileprovider)更改权限,以便 snapchat 可以访问它。这是代码:

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("image/jpg");
            sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            File newFile = new File(getContext().getCacheDir(), "share_score.jpg");
            Log.d(getClass().getSimpleName(), newFile.getAbsolutePath());
            Uri image = FileProvider.getUriForFile(getContext(), "com.test.fileprovider", newFile);
            try {
                // create bitmap screen capture
                View v1 = v.getRootView();
                v1.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                v1.setDrawingCacheEnabled(false);
    
                FileOutputStream outputStream = new FileOutputStream(newFile);
                int quality = 100;
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Throwable e) {
                // Several error may come out with file handling or OOM
                e.printStackTrace();
            }
            sharingIntent.putExtra(Intent.EXTRA_STREAM, image);
            startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));
    

    【讨论】:

      【解决方案2】:

      我在 Instagram 上使用此代码并且工作正常:

      AssetManager assetFiles = getAssets();
      InputStream istr = null;
      Bitmap image = null;
      
      try {
          istr = assetFiles.open("img.jpg");
      } catch (IOException e) {
          e.printStackTrace();
      }
      
      image = BitmapFactory.decodeStream(istr);
      
      String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), image, "Share Text", null);
      Uri bmpUri = Uri.parse(pathofBmp);
      
      Intent sendIntent = new Intent();
      sendIntent.setAction(Intent.ACTION_SEND);
      sendIntent.putExtra(Intent.EXTRA_TEXT, "Share Text");
      sendIntent.setType("image/png");
      sendIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
      sendIntent.setPackage("com.instagram.android");
      startActivity(sendIntent);
      

      我希望这个 sn-p 对你有所帮助。

      【讨论】:

      • 感谢您的回答,但我已经自己解决了问题
      猜你喜欢
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多