简体   繁体   English

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

[英]Picking a photo from gallery and show in a image view

I have an app, which has a button to select a photo from your gallery and it works fine and after selecting the image my app show came back to the activity and shows the image in an image View. 我有一个应用程序,它有一个按钮,可以从您的图库中选择一张照片并且工作正常,选择图像后,我的应用程序显示回到活动并在图像视图中显示图像。

Every is working fine but sometimes ,when i select some particular images the preview is not showing. 每个都工作正常,但有时,当我选择一些特定的图像时,预览没有显示。 I have also tried to compress the image still its not working 我也试图压缩图像仍然无法正常工作

My code is below.. In onCreate() 我的代码在下面.. 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);

    }
});

In onActivityResult(int requestCode, int resultCode, Intent data) 在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));

} 

Try like this 试试这样吧

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);

            }

And

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);
    }

I run into similar problems like getting cursor uri from resource, open stream, set bitmap etc. And it has bugs all the time. 我遇到类似的问题,如从资源,开放流,设置位图等获取游标uri。它一直有bug。

So I searched libraries and found image-chooser-library library. 所以我搜索了库并找到了image-chooser-library库。

I bet you would like to try this project from image-chooser-library 我打赌你想从image-chooser-library尝试这个项目

It is very easy to use and solves all those nitty gritty problems for you, like images from picasa etc. 它非常易于使用并为您解决所有这些细节问题,例如来自picasa等的图像。

Hopefully it is useful for you. 希望它对你有用。

The way you're trying to load a bitmap in onActivityResult() is not absolutely right. 你试图在onActivityResult()加载位图的方式并不是绝对正确的。 Sometimes you will not be able to open an image and your application can crash. 有时您将无法打开图像,您的应用程序可能会崩溃。 You'd better use code like this: 你最好使用这样的代码:

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
        }
    }
}

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

imageView.setImageURI(selectedImage);

It work for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM