在我的应用中,我处理来自其他应用的图像/视频共享。
问题是,如果我想处理来自图库的图像,它可以很好地跟随:
Uri fileUri = (Uri) data.getParcelableExtra(Intent.EXTRA_STREAM);
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(mChatView.returnContext(), fileUri, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
File selectedImage = new File(cursor.getString(column_index));
我得到了我想要的文件。
问题是图像是通过我得到的其他应用程序(Whatsapp / Fb Messenger)共享的:
cursor == null
Whatsapp URI 的示例:
fileUri = file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/Sent/IMG-20160616-WA0000.jpg
如何访问该文件?
- 在Nexus 5上测试 - Android v 6.0.1
答案 0 :(得分:0)
这样做:
String uri = data.getParcelableExtra(Intent.EXTRA_STREAM).toString();
Uri fileUri = (Uri) data.getParcelableExtra(Intent.EXTRA_STREAM);
File selectedImage;
if (uri.startsWith("file://")) {
selectedImage = new File(fileUri.getPath());
}
else
{
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(mChatView.returnContext(), fileUri, projection, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
selectedImage = new File(cursor.getString(column_index));
}