java - MediaStore.Images.Media.getBitmap 意外失败

标签 java android android-intent bitmap android-studio

我正在构建一个 Android 应用程序,可以从照片库获取图像。

如果可以的话我有两个问题:

1)

 Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String action = intent.getAction();

        Uri uri = null;

        // if this is from the share menu
        if (Intent.ACTION_SEND.equals(action)) {
            if (extras.containsKey(Intent.EXTRA_STREAM))
            {
                try
                {
                    // Get resource path from intent callee
                     uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);

我有这个 uri:

内容://media/external/images/media/27031

但是我的代码在这里失败,没有任何信息(它只是崩溃),没有日志猫,没有控制台错误。

 try {
            if (uri !=null)
            {

            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

            imageView.setImageBitmap(bitmap);
            }

        } catch (IOException e) {
            e.printStackTrace();

            Log.e(this.getClass().getName(), e.toString());
        }

我该如何解决这个问题?

(适用于此网址:Uri uri = Uri.parse("content://media/external/images/media/22719");

2) 我计划将所有 uri 保存在数据结构中。这是推荐的方式吗?

还是将照片保存在本地应用程序文件夹中更好?

更新

我认为问题在于失败的图像位于外部 SD 上,而后续图像位于本地 SD 上。

如何启用从外部 SD 获取图像?

最佳答案

一些用于从照片上的“共享”操作获取位图的代码...

    Object obj = null;

        if (Intent.ACTION_SEND.equals(_action) && _type != null) {
            obj = _bundle.get("android.intent.extra.STREAM");
            if( getIntent().getType().startsWith("image/") && obj != null){
              if (obj instanceof android.net.Uri){ get((Uri)obj);}}

  get(Uri uri){
      Bitmap _bit = BitmapFactory.decodeStream(
       getContentResolver().openInputStream(uri),null,options);
}

关于java - MediaStore.Images.Media.getBitmap 意外失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793509/

相关文章:

java - 如何自定义验证失败返回的字段名称?

java - MYSQL 中特定列中的重复项显示为空

Java LocalDateTime minusYears 为浮点年数

android - Xamarin Android - 链接器和 ServiceStack.Text

android - Lollipop : making my activity stay in the task that launched the share intent

android - 使用默认的android图像查看器显示图片

java - 在 `n` 和 `java.util.PriorityQueue` 中插入 `initialCapacity=n` 元素的时间复杂度

Android Hierarchy Viewer 无法在设备模拟器上加载窗口数据。字符串索引超出范围 173

android - 在 ConstraintLayout 中自动调整 TextView

android - 在android中获取存储信息