从android中的图像选择器保存位图中的图像时检索错误

时间:2017-03-26 16:46:15

标签: android bitmap

我正在使用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:无法解码流:java.io.FileNotFoundException:/ external / images / media / 45(没有此类文件或目录)”

我也尝试了以下解决方案,但这也无效。

 //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我/好的: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:at 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)

1 个答案:

答案 0 :(得分:0)

问题是您需要从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()方法,如上所述返回路径。在位图工厂中使用此路径。我已经检查过上面的代码中没有崩溃。希望它有所帮助:)

相关问题