如何将图像从 Gallery 显示到 ImageView?

Posted

技术标签:

【中文标题】如何将图像从 Gallery 显示到 ImageView?【英文标题】:How to show image from Gallery to ImageView? 【发布时间】:2015-09-22 09:47:13 【问题描述】:

我想将从图库中获取的图像显示到图像视图中。但这并没有发生。我不明白为什么。我可以很好地使用相机功能并从相机的图像视图中看到图像,但不能从图库中看到图像。请大家帮帮我好吗?

这是我的代码

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 中。我已经尝试以与“CAMERA_REQUEST”相同的方式处理“SELECT_PHOTO”的情况。也没有成功。

【问题讨论】:

【参考方案1】:

也许BitmapFactory.decodeFile(picturePath) 返回空值。检查。并检查您是否在 AndroidManifest.xml 中添加了权限:

android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE

BitmapFactory.decodeFile(String filePath) 在文档中: http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)

返回: 生成的解码位图,如果 path 为 null 或无法解码,则为 null

【讨论】:

以上是关于如何将图像从 Gallery 显示到 ImageView?的主要内容,如果未能解决你的问题,请参考以下文章

如何将图像从一个活动发送到从 android Gallery 中选择的另一个活动?

即使从Android中的Gallery中删除后,是否可以将捕获的图像保留在应用程序存储中?

如何在 Gallery App IOS 中显示文档目录中的图像?

如何显示来自相机的捕获图像并从文件路径显示在图像视图中? [复制]

javascript 如何在Magnific Popup Gallery中显示图像和视频

如何像 Gallery 一样实现 Horizo​​ntalScrollView?