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

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

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

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

MainActivity

  1. //Open phone gallery
  2. private void getImageFromGallery(){
  3. Intent i = new Intent(Intent.ACTION_PICK,
  4. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  5. startActivityForResult(i, GALLERY_REQUEST);
  6. }
  7. @Override
  8. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  9. super.onActivityResult(requestCode, resultCode, data);
  10. //Check if the intent was to pick image, was successful and an image was picked
  11. if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != data) {
  12. Uri selectedImage = data.getData();
  13. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  14. Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
  15. cursor.moveToFirst();
  16. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  17. String picturePath = cursor.getString(columnIndex);
  18. cursor.close();
  19. ImageView imageView = (ImageView) findViewById(R.id.the_grid_image_preview);
  20. imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  21. }
  22. }

答案1

得分: 0

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

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

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

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

  1. private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
  2. Dexter.withActivity(activity)
  3. .withPermissions(
  4. android.Manifest.permission.READ_EXTERNAL_STORAGE,
  5. android.Manifest.permission.WRITE_EXTERNAL_STORAGE
  6. )
  7. .withListener(object : BaseMultiplePermissionsListener() {
  8. override fun onPermissionsChecked(response: MultiplePermissionsReport) {
  9. if (response.areAllPermissionsGranted()) {
  10. onPermissionGranted()
  11. }
  12. }
  13. })
  14. .check()
  15. }

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

  1. ...
  2. checkFilePermissions {
  3. getImageFromGallery()
  4. }
英文:

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:

  1. private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
  2. Dexter.withActivity(activity)
  3. .withPermissions(
  4. android.Manifest.permission.READ_EXTERNAL_STORAGE,
  5. android.Manifest.permission.WRITE_EXTERNAL_STORAGE
  6. )
  7. .withListener(object : BaseMultiplePermissionsListener() {
  8. override fun onPermissionsChecked(response: MultiplePermissionsReport) {
  9. if (response.areAllPermissionsGranted()) {
  10. onPermissionGranted()
  11. }
  12. }
  13. })
  14. .check()
  15. }

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

  1. ...
  2. checkFilePermissions {
  3. getImageFromGallery()
  4. }

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:

确定