gpt4 book ai didi

android - 如何通过在 Android 中使用内容 uri(_not_ 文件 uri)的 Intent 传递单个图像

转载 作者:太空狗 更新时间:2023-10-29 14:28:46 24 4
gpt4 key购买 nike

(我已经阅读了很多类似的问题,但请耐心等待)

我需要从一个 Activity (自定义相机 Activity )发送一张图片,其中第二个 Activity 是通过 Google API 将图片上传到 Picasa 网络相册。

我发现的每个例子都是这样的:

File f = new File(cacheDir, "image_name.jpg");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);

当我使用标准的 Android Picasa 上传 Activity (或任何其他共享应用程序)时,这工作得很好。我也可以通过 the Picasa example 上传照片我正在使用的应用程序,当从图库/相机等共享图像时。

但我无法弄清楚如何构建一个使用“content://---”uri 的 Intent 并将其传递给另一个应用程序(既不是此示例应用程序也不是 Picasa 标准应用程序).​​..

具体来说:如何创建与以下代码兼容的 Intent(即使用“content://”uri 而不是“file://”uri)?

  static class SendData {
String fileName;
Uri uri;
String contentType;
long contentLength;

SendData(Intent intent, ContentResolver contentResolver) {
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = this.uri = (Uri) extras.get(Intent.EXTRA_STREAM);
String scheme = uri.getScheme();

if (scheme.equals("content")) {
Cursor cursor = contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
this.fileName = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
this.contentType = intent.getType();
this.contentLength = cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media.SIZE));
}
}
}
}
  • 手动从文件 uri 检索文件信息会导致应用中使用的 Google Http 请求出现 NullPointerException。

硬编码内容 uri 有效。例如:

 uploadIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://media/external/images/media/11"));

最佳答案

与媒体文件相关的信息由 MediaStore 存储,它是一个 ContentProvider (http://developer.android.com/reference/android/provider/MediaStore.Images.html)

MediaStore.Images.DATA 列对应文件路径,MediaStore.Images._ID 列对应ID。

您需要查询与您的文件路径对应的 ID,然后从中创建一个 ContentUri(如果图像在外部存储上,它将是 MediaStore.Images.Media.EXTERNAL_CONTENT_URI + id,我会尝试想出一种更好的方法将 ID 转换为内容 URI)。

http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#query(android.content.ContentResolver, android.net.Uri, java.lang.String[]

关于android - 如何通过在 Android 中使用内容 uri(_not_ 文件 uri)的 Intent 传递单个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073979/

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