【问题标题】:Saving image file path in a member variable?将图像文件路径保存在成员变量中?
【发布时间】:2020-04-26 09:29:04
【问题描述】:

我正在尝试创建简单的联系人应用程序。自然有Contact 类的成员变量mCurrentPhotoPath。负责创建联系人的活动可以通过以下方式从图库中选择图片:

  final Intent pickImage = new Intent(Intent.ACTION_GET_CONTENT);
  pickImage.setType("image/*")
  /*some code...*/
  galleryButton.setOnClickListener((View v) -> {
        startActivityForResult(pickImage, REQUEST_GALLERY_PHOTO);
    });

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

    if (requestCode == REQUEST_GALLERY_PHOTO && resultCode == RESULT_OK){
        photoTaken = true;

        Uri selectedImage = data.getData();
        Log.i("path", selectedImage.getPath()) //prints out: /document/image:292562
        mPhotoView.setImageURI(selectedImage);

我能够在 ImageView (mPhotoView) 中显示选定的图像。

但是,当我尝试以不同方式设置 Intent 时,我得到了完整路径,但我无法从该路径重新创建文件,我得到 FileNotFoundException

final Intent pickImage = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

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

        if (requestCode == REQUEST_GALLERY_PHOTO && resultCode == RESULT_OK){
            photoTaken = true;

             Uri selectedImage = data.getData();
             String[] filePathColumn = {MediaStore.Images.Media.DATA};

             if (selectedImage != null) {
                 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                 if (cursor != null) {
                      cursor.moveToFirst();

                      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                      String picturePath = cursor.getString(columnIndex);
                      Log.i.("TAG", picturePath); //I get, /storage/emulated/0/Pictures/Viber/IMG-bbd54c96cc971a1700f92205937014c8-V.jpg
                      mPhotoFile = new File(picturePath);
                      cursor.close();
                      updatePhotoView(); //This method recreates image based on exact file path and witdh & height of ImageView where the picture is going to be placed;
                 }
            }

         }

这里是updatePhotoView()

private void updatePhotoView(int imageWidth, int imageHeight) {
        if (mPhotoFile == null || !mPhotoFile.exists()) {
            mPhotoView.setImageDrawable(null);

        } else {
            Bitmap bitmap = PictureUtils.getScaledBitmap(mPhotoFile.getPath(),imageWidth, imageHeight); // imageWidth & imageHeight are member variables of actiivty...

            mPhotoView.setImageBitmap(bitmap);
        }
    }

我很确定这个功能可以工作,因为当我实现从相机拍照的选项时(我使用 getExternalFilesDir() 创建了文件,并且在任何其他活动中当我传递了mCurrentPhotoPathgetScaledBitmap() 的字符串值时管理重新创建图像)。

这里是getScaledBitmap()

public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
        // Read in the dimensions of the image on disk
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;
        // Figure out how much to scale down by
        int inSampleSize = 1;
        if (srcHeight > destHeight || srcWidth > destWidth) {
            if (srcWidth > srcHeight) {
                inSampleSize = Math.round(srcHeight / destHeight);
            } else {
                inSampleSize = Math.round(srcWidth / destWidth);
            }
        }
        options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;
        // Read in and create final bitmap
        return BitmapFactory.decodeFile(path, options);
}

【问题讨论】:

  • 能否添加`updatePhotoView()'方法的代码?
  • 好的,先生,我马上更新。
  • @Rajnishsuryavanshi 先生,我已经编辑了我的帖子 :)
  • 您应该保存 data.getData().toString(),内容方案,以便您可以在需要时构造您的 uri。如你所见,data.getData().getPath() 没用。
  • 尝试在您的getScaledBitmap() 方法中直接使用picturePath,而不是创建文件并获取路径mPhotoFile = new File(picturePath);

标签: android file android-intent filepath image-gallery


【解决方案1】:

如果有人偶然发现类似的问题。我通过使用 Glide 库解决了获取图像的问题。看看吧,一堆教程。我猜这个load 函数正在后台进行繁重的工作。好东西!下面是代码的样子:

Glide.with(context)
                .load(imageFilePath)
                .apply(new RequestOptions().centerCrop())
                .override(imageView.getWidth(), imageView.getHeight())
                .into(imageView);

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 2021-10-31
    • 2016-04-06
    • 2012-01-18
    • 2020-06-23
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多