gpt4 book ai didi

android - 尝试从 Google 相册中选择视频时出现空值

转载 作者:行者123 更新时间:2023-11-29 02:35:30 25 4
gpt4 key购买 nike

尝试从 Google 相册中选择视频时出现 null
如果视频也存储在您的外部存储器但它不起作用,则它可以工作,
如果它只在谷歌照片上可用。我尝试了很多解决方案,但不知道如何实现。下面是我用来获取 URI 的类。

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 打开并在您的 Activity 中显示 Google Photo 发送给您的照片。你的代码是 Not Acceptable 。尝试在 SO 中搜索此主题。讨论了很多次。

            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 照片应用程序。 如果 Google Photos 将所需文件保存在云中的某个位置,您必须在后台线程中从 is 加载数据以避免 主线程上的网络 异常。

关于android - 尝试从 Google 相册中选择视频时出现空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47362753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com