当前位置: 首页 > 知识库问答 >
问题:

从图库获取图像并在ImageView中显示

汪欣德
2023-03-14
    btn_image_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getImageFromAlbum();
        }
    });
   private void getImageFromAlbum(){
    try{
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }catch(Exception exp){
        Log.i("Error",exp.toString());
    }
}
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, 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();

        try {
            bmp = getBitmapFromUri(selectedImage);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        image_view.setImageBitmap(bmp);

        //to know about the selected image width and height
        Toast.makeText(MainActivity.this, image_view.getDrawable().getIntrinsicWidth()+" & "+image_view.getDrawable().getIntrinsicHeight(), Toast.LENGTH_SHORT).show();
    }

}

但是宽度和高度较低的图像成功地加载到图像视图中!

有人能告诉我代码有什么问题吗?我做错了什么?我只想从库中导入相机图像,并在图像视图中显示它们!

共有1个答案

郭兴平
2023-03-14

你可以试试这个。

将此代码粘贴到按钮单击事件中。

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

下面的代码是你的活动结果

@Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);


        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }
 类似资料:
  • 问题内容: 我需要在手机中打开我的画廊,然后选择一张在活动中在imageview中打开的图片..没什么困难..但是我在模拟器(genymotion)中有完美的代码和thise代码可以运行..但是在小米Mi4上却没有。 打开画廊选择项目,什么也没有。 我没有更多的电话了:( 我尝试下载一些与此主题相关的示例,并且每个示例都是相同的。 您是否有一些从图库中选择图像并在imageview中显示的项目?如

  • 我想使用共享命令从图库获取图像。 我目前的代码是: imageUri 的值为:content://media/external/images/media/37 但是函数“openInputStream”会抛出错误“java.io.FileNotFoundException”。 通过下面的函数,我得到了图像的真实路径。 但我不知道如何将其转换为位图。

  • 我的代码在下面..在中 在onActivityResult(int requestCode,int resultCode,Intent data)中

  • 我想在JavaFX的ImageView中显示图像。图像应该从SQLite数据库中获取,并在场景加载时显示。我使用以下代码片段从数据库获取代码,并在之前创建的ImageView中显示它。但是图像没有按预期显示。我做错了吗?P. S.我测试了改变路径在两个和当读取文件从目录到。但似乎没有什么奏效。但是当我手动将一个图像文件放在src目录中并尝试读取它时,它就像一个魅力。但是我想展示一张从数据库中提取的

  • 我是android的新手,在从服务器上的mysql数据库检索图像url时遇到了问题,我可以使用Hashmap检索字符串,但无法从数据库中检索图像url 我已经使用适配器类链接listview、imageview、textview等 任何人可以检查下面的代码,这样我可以检索图像路径,并链接到imageView和显示图像从数据库插入路径。 mainactivity.java sample.php ac