如何显示从画廊到ImageView的形象? [英] How to show image from Gallery to ImageView?

查看:158
本文介绍了如何显示从画廊到ImageView的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示从画廊到的ImageView拍摄的图像。但是,这没有发生。我不明白为什么。我可以用相机功能就好了,看到从相机的imageview的形象,而不是从画廊所采取的之一。你们能帮助我,好吗?

I want to show the image taken from the gallery into the imageview. But this is not happening. I don't understand why. I can use the camera feature just fine and see the image in the imageview from the camera, but not the one taken from the gallery. Can you guys help me out, please?

下面是我的code

protected Button mFromCamera;
protected Button mFromGallery;
protected ImageView mImageView;

private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PHOTO = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Initialize ImageView
    mImageView = (ImageView) findViewById(R.id.ImgPrev);
    //Initialize Camera
    mFromCamera = (Button) findViewById(R.id.FromCamera);

    //use camera
    mFromCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        } //use camera end

    });

    //initialize button
    mFromGallery = (Button) findViewById(R.id.FromGallery);

    //pick a photo
    mFromGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });//pick a photo end
}



//previewing Image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        //from the gallery
        case SELECT_PHOTO:
            if (requestCode == SELECT_PHOTO && 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();

                mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
            break;
        //from the camera
        case CAMERA_REQUEST:
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                mImageView.setImageBitmap(photo);
            }
            break;
    }
}//Preview Image End

唯一的问题是,从图库中选择的图像中没有的ImageView所示。我试过治疗的SELECT_PHOTO以同样的方式为我所用CAMERA_REQUEST完成的情况。并没有制定出为好。

The only problem is, the image selected from the gallery is not shown in the ImageView. I've tried treating the case for the "SELECT_PHOTO" the same way as what I've done with "CAMERA_REQUEST". and it didn't work out as well.

推荐答案

也许 BitmapFactory.de codeFILE(picturePath)返回null。核实。而且检查你是否在AndroidManifest.xml中添加的权限:

Maybe BitmapFactory.decodeFile(picturePath) returns null. Check it. And check if you added permissions in AndroidManifest.xml:

android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE

BitmapFactory.de codeFILE(字符串文件路径)的文档:
<一href=\"http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cFile(java.lang.String)\" rel=\"nofollow\">http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cFile(java.lang.String)

返回:
所产生的德codeD位图,或如果路径为空或不能去codeD。

Returns: the resulting decoded bitmap, or null if path is null or could not be decoded.

这篇关于如何显示从画廊到ImageView的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆