从图库中选取一张照片并在图像视图中显示

时间:2014-01-15 11:33:02

标签: android image android-gallery

我有一个应用程序,它有一个按钮,可以从您的图库中选择一张照片,并且工作正常,选择图像后,我的应用程序节目又回到了活动状态,并在图像视图中显示图像。

每个都工作得很好但有时候,当我选择一些特定的图像时,预览没有显示。我还试图压缩图像仍然无法正常工作

我的代码在下面.. onCreate()

galeryBtn=(Button)findViewById(R.id.buttonGallery);
galeryBtn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(i, RESULT_LOAD_IMAGE);

    }
});

在onActivityResult(int requestCode,int resultCode,Intent data)

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    // String picturePath contains the path of selected Image

    // Show the Selected Image on ImageView
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

} 

4 个答案:

答案 0 :(得分:5)

试试这个

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        switch (requestCode) {
                case RESULT_LOAD_IMAGE:
            if (resultCode == Activity.RESULT_OK) {

                Uri selectedImage = intent.getData();
                try {
                    Bitmap bitmapImage =decodeBitmap(selectedImage );
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                                // Show the Selected Image on ImageView
                    ImageView imageView = (ImageView) findViewById(R.id.imgView);
                    imageView.setImageBitmap(bitmapImage);

            }

并且

public  Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
    }

答案 1 :(得分:5)

我遇到类似的问题,例如从资源,开放流,设置位图等获取游标uri。它一直有bug。

所以我搜索了库并找到了image-chooser-library库。

我打赌你想从image-chooser-library

尝试这个项目

它非常易于使用并为您解决所有这些细节问题,例如来自picasa等的图像。

希望它对你有用。

答案 2 :(得分:3)

您尝试在onActivityResult()中加载位图的方式并非绝对正确。有时您将无法打开图像,您的应用程序可能会崩溃。你最好使用这样的代码:

Uri imageUri = data.getData();
InputStream imageStream = null;
try {
    imageStream = getContentResolver().openInputStream(imageUri);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
} catch (FileNotFoundException e) {
    // Handle the error
} finally {
    if (imageStream != null) {
        try {
            imageStream.close();
        } catch (IOException e) {
            // Ignore the exception
        }
    }
}

答案 3 :(得分:0)

在imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))之后添加;

imageView.setImageURI(selectedImage);

它对我有用。