无法接收分享图片 - Android 6.0
Unable to receive share image - Android 6.0
我正在使用 aFileChooser 提供的代码来获取我的应用程序中的共享图像。来自图库的图片工作正常,但如果我使用 Google Chrome 内的图片并尝试分享它,它会给我一个 NPE,因为我的 imagePath
为空。
String imagePath = getPath(getActivity(), imageUri);
我的 uri
从这个代码被识别为 MediaStore (and) general:
else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
但是在 getDataColumn()
我的光标转储如下:
08-24 12:00:58.196 13186 13256 ReceivePhotos D Cursor is: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@e110803
08-24 12:00:58.196 13186 13256 ReceivePhotos D 0 {
08-24 12:00:58.196 13186 13256 ReceivePhotos D _data=null
08-24 12:00:58.196 13186 13256 ReceivePhotos D }
08-24 12:00:58.196 13186 13256 ReceivePhotos D <<<<<
08-24 12:00:58.196 13186 13256 ReceivePhotos D Cursor column index is: 0
getDataColumn()方法:
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);
Log.d("ReceivePhotos", " Cursor is: " + DatabaseUtils.dumpCursorToString(cursor));
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
Log.d("ReceivePhotos", " Cursor column index is: " + column_index);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
ImageUri 日志
08-24 12:07:32.780 13629 13696 ReceivePhotos D Image uri: content://com.android.chrome.FileProvider/images/screenshot/1472011649310784004280.jpg
Phone & OS 详情
索尼 E5823 Android 6.0.1
您不能也不应该尝试获取与 URI 对应的基础路径 - 在绝大多数情况下,您的应用永远无法访问路径本身,而只能通过 URI。
幸运的是,您可以直接从 URI 中获取图像的二进制数据:
InputStream in;
Bitmap bitmap = null;
try {
in = getContentResolver().openInputStream(imageUri);
// You could do anything with the InputStream.
// Here we'll just get the Bitmap at full size
bitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
// Something went wrong
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
// Now you have a Bitmap.
我正在使用 aFileChooser 提供的代码来获取我的应用程序中的共享图像。来自图库的图片工作正常,但如果我使用 Google Chrome 内的图片并尝试分享它,它会给我一个 NPE,因为我的 imagePath
为空。
String imagePath = getPath(getActivity(), imageUri);
我的 uri
从这个代码被识别为 MediaStore (and) general:
else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
但是在 getDataColumn()
我的光标转储如下:
08-24 12:00:58.196 13186 13256 ReceivePhotos D Cursor is: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@e110803
08-24 12:00:58.196 13186 13256 ReceivePhotos D 0 {
08-24 12:00:58.196 13186 13256 ReceivePhotos D _data=null
08-24 12:00:58.196 13186 13256 ReceivePhotos D }
08-24 12:00:58.196 13186 13256 ReceivePhotos D <<<<<
08-24 12:00:58.196 13186 13256 ReceivePhotos D Cursor column index is: 0
getDataColumn()方法:
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);
Log.d("ReceivePhotos", " Cursor is: " + DatabaseUtils.dumpCursorToString(cursor));
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
Log.d("ReceivePhotos", " Cursor column index is: " + column_index);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
ImageUri 日志
08-24 12:07:32.780 13629 13696 ReceivePhotos D Image uri: content://com.android.chrome.FileProvider/images/screenshot/1472011649310784004280.jpg
Phone & OS 详情
索尼 E5823 Android 6.0.1
您不能也不应该尝试获取与 URI 对应的基础路径 - 在绝大多数情况下,您的应用永远无法访问路径本身,而只能通过 URI。
幸运的是,您可以直接从 URI 中获取图像的二进制数据:
InputStream in;
Bitmap bitmap = null;
try {
in = getContentResolver().openInputStream(imageUri);
// You could do anything with the InputStream.
// Here we'll just get the Bitmap at full size
bitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
// Something went wrong
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
// Now you have a Bitmap.