Sharing an image with Google+ app using Intent.ACTION_SEND and Intent.EXTRA_STREAM

后端 未结 3 1078
借酒劲吻你
借酒劲吻你 2020-12-13 07:51

My app generates images that a user can save or share with others. The code below works for most apps: Messenger, Facebook, Dropbox, email, etc. Meaning, the image is load

相关标签:
3条回答
  • 2020-12-13 08:11

    Thanks for raising this question!

    Yes it seems that Google+ only accepts media from content providers (content:// uris), not file:// uris. So we need to put the image into MediaStore first. An easier way to do this is:

    MediaStore.Images.Media.insertImage(context.getContentResolver(), tmpFile.getAbsolutePath(), tmpFile.getName(), null);
    
    0 讨论(0)
  • 2020-12-13 08:32

    The reason why you are getting this error is because of "on the sd card" is NOT "on your device".

    A discussion about this issue can be found here: https://groups.google.com/a/googleproductforums.com/forum/#!topic/google-plus-discuss/pdlOTM8JEXg/discussion

    The workaround via the MediaStore seems to be the only solution until Google updates the Google+ app with a fix.

    Update: The solution mentioned above only works if you want to share an image a single time. If you call ContentResolver.insert(...) multiple times, you will end up with the image showing up multiple times in your gallery. I came up with the following solution that tries to get the existing id of the image and if it doesn't exist, you can insert it...

    Cursor c = getActivity().getContentResolver()
                .query(Images.Media.EXTERNAL_CONTENT_URI,
                        null,
                        Images.Media.DATA + "=?",
                        new String[] { filePath }, null);
        if (c.getCount() > 0 && c.moveToFirst()) {
            Uri shareUri = Uri.withAppendedPath(
                    Images.Media.EXTERNAL_CONTENT_URI,
                    ""
                            + c.getInt(c
                                    .getColumnIndex(Images.Media._ID)));
            c.close();
    
            startActivity(Intent.createChooser(
                    new Intent(Intent.ACTION_SEND)
                            .putExtra(Intent.EXTRA_STREAM,
                                    shareUri).setType(
                                       "image/*"), ""));
        } else {
        // ContentResolver.insert(...) and use that uri...
        }
    
    0 讨论(0)
  • 2020-12-13 08:33

    Great catch! You can specify the MediaStore Uri (which looks like content://media/external/images/media/42) instead of the absolute path on the file system.

    Example:

    public class MyActivity extends Activity {
      ...
      static final int IMAGE_REQUEST = 0;
    
      protected void pickImage() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, IMAGE_REQUEST);
      }
    
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_REQUEST) {
          Uri uri = data.getData();
    
          Intent intent = new Intent(Intent.ACTION_SEND);
          intent.setType("image/png");
          // uri looks like content://media/external/images/media/42
          intent.putExtra(Intent.EXTRA_STREAM, uri);
          startActivity(Intent.createChooser(intent , "Share"));
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题