How to implement a ContentProvider for providing image to Gmail, Facebook, Evernote, etc

前端 未结 3 1211
南方客
南方客 2021-01-05 11:00

My previous question (Is it possible to share an image on Android via a data URL?) is related to this question. I have figured out how to share an image from my application

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 11:55

    I had issues with sharing to some email and messaging clients because of the query method. Some recipient apps send in null for the projection parameter. When that happens, your code throws a NullPointerException. The NPE is easy to solve. However, the apps that send null still require some information back. I still can't share to Facebook, but I can share to all other apps I've tested using:

    EDIT I also cannot get it to work with Google Hangout. With that, at least, I get a toast indicating You can't send this file on Hangouts. Try using a picture. See also this question: Picture got deleted after send via Google hangout. I assume this is because I'm using private content and Hangouts can't / won't accept it for some reason.

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        if (projection == null) {
            projection = new String[] {
                    MediaStore.MediaColumns.DISPLAY_NAME,
                    MediaStore.MediaColumns.SIZE,
                    MediaStore.MediaColumns._ID,
                    MediaStore.MediaColumns.MIME_TYPE
            };
        }
    
        final long time = System.currentTimeMillis();
        MatrixCursor result = new MatrixCursor(projection);
        final File tempFile = generatePictureFile(uri);
    
        Object[] row = new Object[projection.length];
        for (int i = 0; i < projection.length; i++) {
    
           if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DISPLAY_NAME) == 0) {
              row[i] = uri.getLastPathSegment();
           } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.SIZE) == 0) {
              row[i] = tempFile.length();
           } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DATA) == 0) {
              row[i] = tempFile;
           } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.MIME_TYPE)==0) {
              row[i] = _mimeType;
           } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DATE_ADDED)==0 ||
                   projection[i].compareToIgnoreCase(MediaStore.MediaColumns.DATE_MODIFIED)==0 ||
                   projection[i].compareToIgnoreCase("datetaken")==0) {
               row[i] = time;
           } else if (projection[i].compareToIgnoreCase(MediaStore.MediaColumns._ID)==0) {
               row[i] = 0;
           } else if (projection[i].compareToIgnoreCase("orientation")==0) {
               row[i] = "vertical";
           }
        }
    
        result.addRow(row);
        return result;
    }
    

提交回复
热议问题