2

When I try to send an image to the server I get an error. On a device with Android 6 and Android 9, everything works well. But when I use a device with Android 10 or BlueStacks 4 emulator I get an error. My Manifesto looks like this:

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

I also tried adding the following tag to the manifest: android:requestLegacyExternalStorage="true" but my problem remains. In the logs I get an error that the Compressor library gets null

My Activity:

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when (requestCode) {
            PermUtil.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS -> {
                if (grantResults.isEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Pix.start(this, Options.init().setRequestCode(100));
                } else {
                    ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL)
                    Toast.makeText(
                        this, "Approve permissions to open Pix ImagePicker",
                        Toast.LENGTH_LONG).show();
                }
            }
        }
    }

// Function that causes an error

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {

            Constants.ACTION_REQUEST_CAMERA ->{
                if (resultCode == Activity.RESULT_OK){

                    returnValue = data!!.getStringArrayListExtra(Pix.IMAGE_RESULTS)!!

                        val f = File(returnValue[0])
                        photoFile = Compressor(this).compressToFile(f)

                    Toast.makeText(this, f.toString(), Toast.LENGTH_LONG).show()
                }
            }
        }
    }

Logcat:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.foodstationapp/com.example.foodstationapp.activities.RegisterActivity}: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200527-WA0000.jpg: open failed: EACCES (Permission denied)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4905)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4946)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200527-WA0000.jpg: open failed: EACCES (Permission denied)
    at libcore.io.IoBridge.open(IoBridge.java:496)
    at java.io.FileInputStream.<init>(FileInputStream.java:159)
    at java.io.FileInputStream.<init>(FileInputStream.java:115)
    at android.media.ExifInterface.initForFilename(ExifInterface.java:2297)
    at android.media.ExifInterface.<init>(ExifInterface.java:1386)
    at id.zelory.compressor.ImageUtil.decodeSampledBitmapFromFile(ImageUtil.java:60)
    at id.zelory.compressor.ImageUtil.compressImage(ImageUtil.java:33)
    at id.zelory.compressor.Compressor.compressToFile(Compressor.java:60)
    at id.zelory.compressor.Compressor.compressToFile(Compressor.java:56)
    at com.example.foodstationapp.activities.RegisterActivity.onActivityResult(RegisterActivity.kt:268)
    at android.app.Activity.dispatchActivityResult(Activity.java:8249)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4898)
    ... 11 more
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7419)
    at libcore.io.IoBridge.open(IoBridge.java:482)
    ... 22 more
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Infernal
  • 202
  • 3
  • 13
  • just a small note, but the `android-studio` tag is only meant to be used when your question is regarding the IDE itself, not just because your project is built using android studio :) – a_local_nobody May 28 '20 at 14:09
  • @a_local_nobody ok sir – Infernal May 28 '20 at 14:11
  • Use file provider https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps – Raj May 28 '20 at 14:25
  • @Raj Sir Provider is already used in my app. Everything is as in your link. But I still get this error – Infernal May 28 '20 at 15:01
  • there is an isssue with PixImagePicker. You should check its issues. It crashes for android 10. https://github.com/akshay2211/PixImagePicker/issues/159 – miraquee Jan 10 '21 at 10:57

1 Answers1

0

With every new Android version Google makes it harder/impossible to use java- File class. Google wants you to use contentprovider with InputStream or OutputStream instead.

If you get the photo of your crash-stacktrace "/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200527-WA0000.jpg" from a gallery or image picker use the original "content:...." uri and load the image through

context.getContentResolver().openInputStream(contentUri);


update:

library 'com.fxn769:pix' = https://github.com/akshay2211/PixImagePicker. It is currently not working any more on android-10: See https://github.com/akshay2211/PixImagePicker/issues/103

The android way (since android-4.4) to pick an image is

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
       PICK_IMAGE);

where you get a "content:"-uri in onActivityResult that you can open through contentresolver

k3b
  • 14,517
  • 7
  • 53
  • 85
  • Sir I'm getting an image using a library 'com.fxn769:pix:1.5.3' I don't really understand how I can use this library and your code together – Infernal May 28 '20 at 14:52