应用程序在 api 23 multidex 在 19 api 崩溃

App crash on api 23 multidex on 19 api

FATAL EXCEPTION: main
                                                                        Process: com.location.abc.kharcha, PID: 2963
                                                                        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/47 }} to activity {com.location.abc.kharcha/com.location.abc.kharcha.Expense_main}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/47 from pid=2963, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                            at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                            at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/47 from pid=2963, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                            at android.os.Parcel.readException(Parcel.java:1599)
                                                                            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                            at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                                                                            at android.content.ContentResolver.query(ContentResolver.java:491)
                                                                            at android.content.ContentResolver.query(ContentResolver.java:434)
                                                                            at com.location.abc.kharcha.Expense_main.onActivityResult(Expense_main.java:403)
                                                                            at android.app.Activity.dispatchActivityResult(Activity.java:6428)
                                                                            at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                            at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

在 19 api

上构建 Gradle 错误
Information:Gradle tasks [:app:assembleDebug]
:appreBuild UP-TO-DATE
:appreDebugBuild UP-TO-DATE
:app:checkDebugManifest
:appreReleaseBuild UP-TO-DATE
:apprepareComAndroidSupportAnimatedVectorDrawable2400Library UP-TO-DATE
:apprepareComAndroidSupportAppcompatV72400Library UP-TO-DATE
:apprepareComAndroidSupportMultidex101Library UP-TO-DATE
:apprepareComAndroidSupportSupportV42400Library UP-TO-DATE
:apprepareComAndroidSupportSupportVectorDrawable2400Library UP-TO-DATE
:apprepareComAndroidVolleyVolley100Library UP-TO-DATE
:apprepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:approcessDebugManifest UP-TO-DATE
:approcessDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac
None of the classes needs to be compiled! Analysis took 0.827 secs. 
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:apprePackageMarkerForDebug
:app:transformClassesWithJarMergingForDebug FAILED
Error:Execution failed for task ':app:transformClasses`enter code here`WithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/http/MessageConstraintException.class
Information:BUILD FAILED
Information:Total time: 8.706 secs
Information:1 error
Information:0 warnings
Informationee complete output in console

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                   // viewImage.setImageBitmap(bitmap);
                    BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
                   // viewImage.setBackgroundDrawable(ob);
                    viewImage.setImageBitmap(bitmap);
                    filePath = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    File_path_check=true;
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(filePath, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else if (requestCode == 2) {
                Uri selectedImage = data.getData();
                String[] FilePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,FilePath, null, null, null);
                c.moveToFirst();
                Log.e("file", DatabaseUtils.dumpCursorToString(c));
                int columnIndex = c.getColumnIndex(FilePath[0]);
                 filePath = c.getString(columnIndex);
                File_path_check=true;
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(filePath));
                Log.e("path image from gallery", filePath+"");
               // viewImage.setImageBitmap(thumbnail);
                BitmapDrawable ob = new BitmapDrawable(getResources(), thumbnail);
                viewImage.setBackgroundDrawable(ob);
            }
        }
    }

正如你的错误所说。

java.lang.SecurityException: Permission Denial:reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2963, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

您必须在 Manifest.xml 文件中添加此权限。

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

还有一件事,如果你在 Api 23 中测试,那么你必须在下面以编程方式检查它。

查看有关权限的文档以获取更多信息:https://developer.android.com/preview/features/runtime-permissions.html