【问题标题】:Android: How to set the photo selected from gallery to a bitmapAndroid:如何将从图库中选择的照片设置为位图
【发布时间】:2015-04-22 16:43:31
【问题描述】:

我在这里要求用户通过代码作为监听器访问画廊:

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

但是,我对如何为所选照片设置变量感到困惑。

将代码设置为所选照片的​​变量应该放在哪里?

谢谢:)

【问题讨论】:

  • 你能得到图片路径吗??

标签: android gallery photo


【解决方案1】:

首先你必须覆盖onActivityResult 以获取文件选择图像的uri

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == SELECT_PHOTO) {

        if (resultCode == RESULT_OK) {
            if (intent != null) {
                // Get the URI of the selected file
                final Uri uri = intent.getData();
                useImage(uri);                   
              }
        }
       super.onActivityResult(requestCode, resultCode, intent);

    }
}

然后定义useImage(Uri)来使用图片

void useImage(Uri uri)
{
 Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
 //use the bitmap as you like
 imageView.setImageBitmap(bitmap);
}

【讨论】:

  • 你最好检查一下onActivityResult的if语句里面的requestCode。
【解决方案2】:

你可以这样做。

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

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

         // Do something with the bitmap


        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}

【讨论】:

  • 但是我应该把这个放在哪里?就在 onCreate 中还是应该把它放在监听器中让按钮选择它?
  • 如果我想保存它创建的 bitmp,我可以在类的开头创建一个名为 bitmap 的变量,然后只说“bitmap = BitmapFactory.decodeFile(imagePath, options);”吗?
  • 另外,感谢您的澄清和帮助:)
  • 我不确定我是否理解您的评论问题。但是您从 galerry 返回的图像已经保存,这就是您返回指向该图像的路径的原因。如果需要,您可以直接复制该文件。但是将其读入位图可能更有用...这取决于您要做什么
【解决方案3】:

Akash Kurian Jose 的替代答案

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

我总是用

fun getBitmap(file: Uri, cr: ContentResolver): Bitmap?{
            var bitmap: Bitmap ?= null
            try {
                val inputStream = cr.openInputStream(file)
                bitmap = BitmapFactory.decodeStream(inputStream)
                // close stream
                try {
                    inputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }catch (e: FileNotFoundException){}
            return bitmap
        }

它适用于图库中的照片和相机中的照片。

关于它的更大问题:Picasso unable to display image from Gallery

使用此方法打开图库:

private void openGallery(){
    if (Build.VERSION.SDK_INT <19){
        Intent intent = new Intent(); 
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
    } else {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
    }
}

然后你就可以读取转换Uri位图使用afro提到ContentResolver.openInputStream或设置图像@ 987654326@

【讨论】:

    【解决方案4】:

    如果要将所选图像显示到任何特定的 ImageView。 假设我们有 RC_PHOTO_PICKER = 1 那么这些代码行应该可以发挥作用

    private void openPhotoPicker() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false);
        startActivityForResult(Intent.createChooser(photoPickerIntent,"Complete Action Using"), RC_PHOTO_PICKER);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK && data != null) {
            Uri pickedImage = data.getData();
            //set the selected image to ImageView
            mImageView.setImageURI(pickedImage);
        }
    }
    

    然后简单地调用 openPhotoPicker() 方法

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多