简体   繁体   English

从任何画廊中挑选图像突然停止工作

[英]Picking images from any gallery suddenly stopped working

my app has suddenly started crashing after trying to pick an image from any gallery.尝试从任何图库中选择图像后,我的应用程序突然开始崩溃。 It has been working with this same code for a very long time.很长一段时间以来,它一直在使用相同的代码。 I prepared it for android 11 lately and after I finished editing the code, it still worked.我最近为 android 11 准备了它,在我完成代码编辑后,它仍然有效。 Today it decided to throw this exception:今天它决定抛出这个异常:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: 
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200915-WA0003.jpg: open failed: EACCES (Permission denied)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.file.to.text, PID: 6149
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/63620/ORIGINAL/NONE/image/jpeg/2126822588 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F63620/ORIGINAL/NONE/image%2Fjpeg/2126822588} }} to activity {*mypackagename*/*mypackagename*.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4846)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4887)
        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:2017)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7403)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
        at com.file.to.text.MainActivity.getBase64String(MainActivity.kt:171)
        at com.file.to.text.MainActivity.onActivityResult(MainActivity.kt:138)
        at android.app.Activity.dispatchActivityResult(Activity.java:8119)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4839)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4887) 
        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:2017) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7403) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

I don't know what is causing it, the permissions to read from the external storage and write to the external storage are allowed and this exception is caused with every gallery.我不知道是什么原因造成的,允许从外部存储读取和写入外部存储的权限,并且每个画廊都会导致此异常。

I'm testing on a device with stock Android 10.我正在使用库存 Android 10 的设备上进行测试。

I've been using this code in my app to pick images galleries:我一直在我的应用程序中使用此代码来选择图片库:

fun pickimage(view: View) {
    //val getIntent = Intent(Intent.ACTION_GET_CONTENT)
    //getIntent.type = "image/*"
    val pickIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    pickIntent.type = "image/*"
    //val chooserIntent = Intent.createChooser(getIntent, "Select Image")
    //chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(pickIntent))
    startActivityForResult(pickIntent, PICK_IMAGE)
    pic.isEnabled = false
}

The onActivityResult(): onActivityResult():

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_CANCELED && resultCode == RESULT_OK) {
            if (requestCode == PICK_IMAGE) {
                val selectedImage = data!!.data
                val picturePath = getPath(applicationContext, selectedImage)
                val encoded = getBase64String(picturePath, press) //press - some integer
            }
        }
}

The getBase64String(): getBase64String():

private fun getBase64String(path: String, press: Int): String {
        val bitmap = BitmapFactory.decodeFile(path)
        val byteArrayOutputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, press, byteArrayOutputStream)
        val byteArray = byteArrayOutputStream.toByteArray()
        return Base64.encodeToString(byteArray, Base64.DEFAULT)
}

I would gladly appreciate any help.我很乐意提供任何帮助。

Uri uri = data.getData();   

String projection [] = {

          MediaStore.Images.Media.DATA
        , MediaStore.Images.Media.DISPLAY_NAME
        , MediaStore.Images.Media.SIZE};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

if ( cursor==null)
{
    Toast.makeText(context, "cursor==null\n\ncould not query content resolver for\n\n" + uri.toString(), Toast.LENGTH_LONG).show();
    
    return;
}

cursor.moveToFirst();
String data = cursor.getString(0);
String displayName = cursor.getString(1);
String size = cursor.getString(2);

Toast.makeText(context, "query() ok\n\n" + uri.toString()
            + "\n\nDISPLAY_NAME: " + displayName
            + "\nDATA: " + data
            + "\nSIZE: " + size
            , Toast.LENGTH_LONG).show();
cursor.close();             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM