将图像文件路径保存在Android的成员变量中吗?

问题描述 投票:0回答:1

我正在尝试创建简单的联系人应用。自然地,存在具有成员变量ContactmCurrentPhotoPath类。一个负责创建联系人的活动,可以通过以下方式选择从图库中选择图像:

  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()创建文件时,以及在其他任何活动中,当我传递了mCurrentPhotoPath的字符串值时,getScaledBitmap()都设法重新创建图像)。

这里是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);
}
android file android-intent filepath image-gallery
1个回答
0
投票

像这样改变你的意图

 final Intent pickImage = new Intent(Intent.ACTION_PICK);
 pickImage.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
 pickImage.putExtra("return-data", true); 

如果它的有用帮助在您的应用程序中将此行添加到您的清单中,则>]

   android:requestLegacyExternalStorage="true"
© www.soinside.com 2019 - 2024. All rights reserved.