【问题标题】:Retrieving error while saving image in bitmap from image picker in android从android中的图像选择器将图像保存在位图中时检索错误
【发布时间】:2017-03-26 16:46:15
【问题描述】:

我正在使用 Photopicker Library Link 从 SD 卡中检索图像集合。我需要将这些图像保存到位图中,以便将其传递给另一个类(该类只接受位图Link)。

在下面的代码中,我正在检索从图像选择器中选择的图像集合。

public byte[] generateGIF(Photo[] collection) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    Bitmap[] allimages = null ; **// Array of Bitmap images**
    int i=0;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    encoder.start(bos);
    for (Photo photo : collection) {
        Log.i("After", "URI: " + photo.getUri());

   //Photo[] is a array of Photo class that holds the array of the images selected via Image Picker.
  // getUri() is a method in Photo class to retrieve the Uri of the Image.
        allimages[i] = BitmapFactory.decodeFile(photo.getUri().getPath(),options);

        i++;
        encoder.addFrame(allimages[i]);
    }
    i=0;
    encoder.finish();
    //Toast.makeText(MainActivity.this, "Saved.", Toast.LENGTH_SHORT).show();
    return bos.toByteArray();
}

执行时我遇到错误。 检索错误“BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /external/images/media/45 (No such file or directory)”

我也尝试了以下解决方案,但也没有用。

 //allimages[i] = MediaStore.Images.Media.getBitmap(this.getContentResolver(),photo.getUri());

下面也试过了

  // allimages[i] = BitmapFactory.decodeFile(photo.getUri().getPath());

请帮我解决这个问题。 提前谢谢...

:Log.i("After", "URI:" + photo.getUri()); 之后的错误日志

03-27 21:16:12.624 2393-2393/com.patel.pritesh.myapplication I/After: URI: content://media/external/images/media/44 03-27 21:16:12.624 2393-2393/com.patel.pritesh.myapplication I/Okay: URI: content://media/external/images/media/44 03-27 21:16:12.624 2393-2393/com.patel.pritesh.myapplication E/BitmapFactory: 无法解码流: java.io.FileNotFoundException: content:/media/external/images/media/44 (没有这样的文件或目录) 03-27 21:16:12.624 2393-2393/com.patel.pritesh.myapplication W/System.err: java.lang.NullPointerException: 尝试写入空数组 03-27 21:16:12.624 2393-2393/com.patel.pritesh.myapplication W/System.err: 在 com.patel.pritesh.myapplication.MainActivity.generateGIF(MainActivity.java:184) 03-27 21:16:12.625 2393-2393/com.patel.pritesh.myapplication W/System.err: at com.patel.pritesh.myapplication.MainActivity.GifCreator(MainActivity.java:155)

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    问题是您需要从 URI 获取绝对路径。使用以下代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE_PHOTO_PICKER) {
            if (resultCode == Activity.RESULT_OK) {
                Photo[] photos = PhotoPicker.getResultPhotos(data);
                Toast.makeText(this, "User selected " + photos.length + " photos", Toast.LENGTH_SHORT).show();
                for (Photo photo : photos) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    BitmapFactory.decodeFile(getRealPathFromURI(this,photo.getUri()),options);
                    Log.i("dbotha", "URI: " + getRealPathFromURI(this, photo.getUri()));
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "Photo Picking Cancelled", Toast.LENGTH_SHORT).show();
            } else {
                Log.i("dbotha", "Unknown result code: " + resultCode);
            }
        }
    }
    
    public String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    

    在您的应用程序中实现 getRealPathFromURI() 方法,如上所示,返回路径。在位图工厂中使用此路径。我在上面的代码中检查了我没有崩溃。希望对你有帮助:)

    【讨论】:

    • hi @mudit pant Image Picker Library 中的照片类只有一种方法。这是getUri()。检查 [这里] (github.com/dbotha/Android-Photo-Picker/blob/master/photopicker/…) 我正在尝试使用 getPath() 方法获取路径。我该如何解决这个问题。?因为我无法直接在此使用 getPath() 或 getAbsolutePath()。
    • 你能发布 Log.i("After", "URI:" + photo.getUri().toString()); 的日志语句吗?您还可以检查 /external/images/media/ 文件夹是否存在?使用 adb shell 然后 cd 到该文件夹​​
    • 我已经编辑了我的答案。问题是从 URI 获取路径。现在可以使用了。
    • 谢谢,@mudit 裤子。这工作得很好..非常感谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2020-11-04
    • 2014-05-11
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多