无法将流图像解码为片段。

huangapple go评论77阅读模式
英文:

Unable to Decode Stream Image to Fragment

问题

以下是翻译后的内容:

代码部分略。

英文:

The Code Works perfectly but Im having problem with the Permission Denied but i already put READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission on Android Manifest any idea. Already Tried this one https://stackoverflow.com/questions/53358582/unable-to-decode-stream-java-io-filenotfoundexception-permission-denied

https://stackoverflow.com/questions/29163444/bitmapfactory-unable-to-decode-stream-null

> E/BitmapFactory: Unable to decode stream:
> java.io.FileNotFoundException:
> /storage/emulated/0/DCIM/Camera/IMG_20201001_125759.jpg (Permission
> denied)

AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

MainActivity

    //Open phone gallery
    private void getImageFromGallery(){
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, GALLERY_REQUEST);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Check if the intent was to pick image, was successful and an image was picked
        if (requestCode == GALLERY_REQUEST && 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();
            ImageView imageView = (ImageView) findViewById(R.id.the_grid_image_preview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

答案1

得分: 0

如果您使用的是 Android 6.0 版本或更高版本,则需要在运行时请求权限:

https://developer.android.com/training/permissions/requesting

为此,我使用一个名为Dexter的便捷包装器,它能够简化操作并且非常易于使用。

然后您可以在 Fragment 中编写以下函数:

private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
    Dexter.withActivity(activity)
        .withPermissions(
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        .withListener(object : BaseMultiplePermissionsListener() {
            override fun onPermissionsChecked(response: MultiplePermissionsReport) {
                if (response.areAllPermissionsGranted()) {
                    onPermissionGranted()
                }
            }
        })
        .check()
}

当您调用 getImageFromGallery() 方法时,只需像这样调用它:

...
checkFilePermissions {
    getImageFromGallery()
}
英文:

If you're using Android version 6.0 or up, you need to ask for permissions in runtime:

https://developer.android.com/training/permissions/requesting

For this I use a nice wrapper called Dexter which simplifies it and is really easy to use.

You can then write this function in your Fragment:

private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
        Dexter.withActivity(activity)
            .withPermissions(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            .withListener(object : BaseMultiplePermissionsListener() {
                override fun onPermissionsChecked(response: MultiplePermissionsReport) {
                    if (response.areAllPermissionsGranted()) {
                        onPermissionGranted()
                    }
                }
            })
            .check()
    }

And in when you call your method getImageFromGallery() just invoke it like this:


...
checkFilePermissions {
    getImageFromGallery()
}

huangapple
  • 本文由 发表于 2020年10月1日 14:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64149941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定