尝试从Google相册中选择视频时,我收到了空格
尝试从Google相册中选择视频时,我收到了null
。
它的作品,如果视频也存储在你的外部存储器但它不工作,
如果它的唯一可用谷歌照片。我尝试了很多解决方案,但不知道如何实现这一点。下面是我用来获取URI的类。尝试从Google相册中选择视频时,我收到了空格
public class FilePath {
private static final DecimalFormat format = new DecimalFormat("#.##");
private static final long MiB = 1024 * 1024;
private static final long KiB = 1024;
public static String getPath(final Context context, String string) {
return getPath(context, Uri.parse(string));
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
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);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getFileSize(File file) {
if (!file.isFile()) {
throw new IllegalArgumentException("Expected a file");
}
final double length = file.length();
if (length > MiB) {
return format.format(length/MiB) + " MB";
}
if (length > KiB) {
return format.format(length/KiB) + " KB";
}
return format.format(length) + " B";
}
}
在此先感谢。
您没有权限阅读Google照片向我发送文件的媒体,我想。您必须使用getContentResolver().openInputStream(uri)
作为InputStream打开,并在您的活动中显示Google照片向您发送的照片。你的代码是不可接受的。尝试搜索这个主题。这是讨论了很多次。
Intent intent = getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction()){
uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri != null) {
InputStream is = getContentResolver().openInputStream(uri);
long fileSize = is.available()
//Do some suff to show stream as a picture in the Activity
}
}
此代码对我的作品与去年谷歌照片应用中使用Android 7.0时,画面存储在手机的内部存储。 如果Google Photos在云中的某个位置保留了所需的文件,则必须在后台线程中加载is
的数据以避免Networking on main thread
异常。
感谢您的建议@isabsent,我试着下面提到的代码,但得到'FileNotFoundException': InputStream inputStream = getContentResolver()。openInputStream(data.getData()); –
您试图查看的照片位于何处? – isabsent
上述代码仅适用于该图片实际位于手机上或谷歌照片上的情况。在我的情况下,图片并非物理位于设备上,它仅位于谷歌照片上。 'uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);'它返回null。 –
从外部或从内部和外部选择视频时遇到问题 –
@Amjad,即时通讯面临的问题,当我尝试从谷歌照片应用程序中选择一个视频。 –