致命例外:ThumbnailManager-1 在将图像共享到 Intent 时

FATAL EXCEPTION: ThumbnailManager-1 while sharing images to Intent

我在使用 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

当我选择要共享的电子邮件时,图像可以正确共享,但是当我 select 发送消息时,图像会失败。请协助。

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

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

  • OpenableColumns.DISPLAY_NAME
  • OpenableColumns.SIZE

如果请求这两列中的 none,它只会 return 一个空行(根本没有任何列)。

在您的情况下,空行会导致 MMS 应用程序在请求索引为 0 的列时崩溃。

那你能做什么?

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

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

  • 检查 projection 数组,如果其中出现 DISPLAY_NAMESIZE 以外的任何列,则抛出 IllegalArgumentException。这应该有效,因为 MMS 应用程序似乎忽略了此异常。不过,这可能会破坏其他应用程序。

  • 检查投影并在请求的 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 标签的实际值(如果图像中存在)。