6

使用 FileProvider 共享图像时出现以下异常。以下是我以前使用的代码。

{

  ArrayList<Uri> files = new ArrayList<Uri>();
  files.add(getImageUriFromCache(context,bitmap,fileName));

}


private void startSharingIntent(ArrayList<Uri> files,String caption){
Intent i=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            i.setType("image/png");
            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            i.putExtra(Intent.EXTRA_SUBJECT, "my app share");
            i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
            i.putExtra(Intent.EXTRA_TEXT,caption);
            i.putExtra(Intent.EXTRA_TITLE,caption);
            context.startActivity(Intent.createChooser(i, "Tital"));
        }

public Uri getImageUriFromCache(Context inContext, Bitmap inImage , String fileId) throws IOException{

                File cachePath = new File(inContext.getFilesDir() , "images/");
                cachePath.mkdirs(); // don't forget to make the directory
                File newFile = new File(cachePath, "im"+fileId+"_post.png");
                newFile.setReadable(true, false);
                FileOutputStream stream = new FileOutputStream(newFile); // overwrites this image every time
                inImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                stream.close();
                Uri contentUri = FileProvider.getUriForFile(inContext, "com.xx.xx.fileprovider", newFile);
                return contentUri;
}

异常的详细堆栈跟踪如下。

08-11 09:02:47.943: I/Choreographer(428): Skipped 31 frames!  The application may be doing too much work on its main thread.
08-11 09:02:48.343: D/dalvikvm(1832): GC_FOR_ALLOC freed 287K, 8% free 4686K/5048K, paused 25ms, total 25ms
08-11 09:02:48.353: I/dalvikvm-heap(1832): Grow heap (frag case) to 5.200MB for 583180-byte allocation
08-11 09:02:48.403: D/dalvikvm(1832): GC_FOR_ALLOC freed 32K, 8% free 5223K/5620K, paused 35ms, total 40ms
08-11 09:02:48.693: E/CursorWindow(1832): Failed to read row 0, column 0 from a CursorWindow which has 1 rows, 0 columns.
08-11 09:02:48.713: W/dalvikvm(1832): threadid=20: thread exiting with uncaught exception (group=0xb1ac1b90)
08-11 09:02:48.773: E/AndroidRuntime(1832): FATAL EXCEPTION: ThumbnailManager-1
08-11 09:02:48.773: E/AndroidRuntime(1832): Process: com.android.mms, PID: 1832
08-11 09:02:48.773: E/AndroidRuntime(1832): java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
08-11 09:02:48.773: E/AndroidRuntime(1832):     at android.database.CursorWindow.nativeGetLong(Native Method)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at android.database.CursorWindow.getLong(CursorWindow.java:507)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at android.database.CursorWindow.getInt(CursorWindow.java:574)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at android.database.CursorWrapper.getInt(CursorWrapper.java:102)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at com.android.mms.ui.UriImage.getOrientation(UriImage.java:546)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at com.android.mms.util.ThumbnailManager$ThumbnailTask.requestDecode(ThumbnailManager.java:490)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at com.android.mms.util.ThumbnailManager$ThumbnailTask.onDecodeOriginal(ThumbnailManager.java:439)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at com.android.mms.util.ThumbnailManager$ThumbnailTask.getBitmap(ThumbnailManager.java:342)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at com.android.mms.util.ThumbnailManager$ThumbnailTask.run(ThumbnailManager.java:252)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-11 09:02:48.773: E/AndroidRuntime(1832):     at java.lang.Thread.run(Thread.java:841)
08-11 09:02:48.793: W/ActivityManager(377):   Force finishing activity com.android.mms/.ui.ComposeMessageActivity

当我选择电子邮件共享时,图像可以正确共享,但当我选择消息传递时失败。请协助。

4

1 回答 1

4

根据消息来源,UriImage(Mms 应用程序的)Android 类尝试读取您提供的图像的方向。它通过queryUri您提供的请求MediaStore.Images.ImageColumns.ORIENTATION列上运行 a 来做到这一点。

我没有找到 FileProvider 类的最新来源,但这个较旧的FileProvider.java版本只支持两个列名:

  • OpenableColumns.DISPLAY_NAME
  • OpenableColumns.SIZE

如果这两个列都没有被请求,它将只返回一个空行(根本没有列)。

在您的情况下,空行会导致 MMS 应用程序在请求具有 index 的列时崩溃0

所以,你可以做什么?

我看到至少三个可能的选择。所有这些都涉及创建您自己的子类,FileProvider该子类会覆盖该query方法。

这取决于您的目标和技能水平,哪一个最适合您。我尝试将它们从“简单”到“相当多的工作”列出。

  • 检查projection数组并抛出除or以外的IllegalArgumentException任何列。这应该可以工作,因为彩信应用似乎忽略了这个异常。不过,这可能会破坏其他应用程序。DISPLAY_NAMESIZE

  • 检查投影并将所请求的ORIENTATION列附加一个值0(表示图像不需要任何旋转)。

    这是一些(未经测试的)代码来实现这一点:

      @Override
      public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
      {
          Cursor cursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
          if (    cursor == null &&
                  projection != null
                  && projection.length == 1
                  && MediaStore.Images.ImageColumns.ORIENTATION.equals(projection[0])) {
              // query from MMS app, return a single row with a 0 value
              final MatrixCursor matrixCursor = new MatrixCursor(projection, 1);
              matrixCursor.addRow(new Integer[]{0});
              cursor = matrixCursor;
          }
          return cursor;
      }
    
  • 检查投影并将请求的ORIENTATION列附加到 EXIF 标记中的实际值(如果图像中存在)。

于 2018-06-24T21:10:08.940 回答