I got following Exception while sharing images using FileProvider. Following is the code I used to.
{
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;
}
Detail stack trace of exception is as follows.
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
Images are shared correctly when I choose email to share, but failed when I select messaging. Please assist.
According to the sources, the Android UriImage
class (of the Mms app) tries to read the orientation of the image you provide. It does that by running a query
on the Uri
you provided asking for the MediaStore.Images.ImageColumns.ORIENTATION
column.
I didn't find recent sources of the FileProvider class, but this older FileProvider.java version only supports two column names:
OpenableColumns.DISPLAY_NAME
OpenableColumns.SIZE
If none of these two columns are requested it will just return an empty row (with no columns at all).
In your case the empty row causes the MMS app to crash when it requests a column with the index 0
.
So what can you do?
I see at least three possible options. All of them involve creating your own Subclass of FileProvider
which overrides the query
method.
It depends on your goal and skill levels which one is best for you. I try to list them from "simple" to "quite a bit of work".
check the projection
array and throw an IllegalArgumentException
if any column other than DISPLAY_NAME
or SIZE
appears in it. This should work because the MMS app seems to ignore this exception. This might break other apps though.
check the projection and append the requested ORIENTATION
column with a value of 0
(indicating the image doesn't need any rotating).
Here is some (untested) code to implement this:
@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;
}
check the projection and append the requested ORIENTATION
column with the actual value from the EXIF tag, if present in the image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With