Home > Software engineering >  App crashes when I select an image from the gallery
App crashes when I select an image from the gallery

Time:11-14

as soon as I select an image from the gallery the app crashes directly and gives me the following error in the console. I have tried many solutions but none have been successful. I hope you can help me. Thanks a lot.

Error message

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/18 flg=0x1 }} to activity {com.tymo.meinkochbuch/com.tymo.meinkochbuch.AddNewRecipe}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4360)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Code RealPathUtil

public static String getRealPathFromURI_API19(Context context, Uri uri) {
        String filePath = "";
        String wholeID = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

        String[] column = {MediaStore.Images.Media.DATA};

        // where id is equal to
        String sel = MediaStore.Images.Media._ID   "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    }

Code on ActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_OPEN_GALLERY:
                switch (resultCode) {
                    case RESULT_OK:
                        if (data.getData() != null){

                            Uri imageData = data.getData();
                            String imageSrc = Files.getRealPathFromURI(this, imageData);
                            ((AddRecipeOne) getSupportFragmentManager().findFragmentById(R.id.frame_container))
                                    .onImageSelected(imageSrc);
                            currentRecipe.setImagePath(imageSrc);
                        }
                        break;
                }
        }
    }

CodePudding user response:

I see that you are using that code to load the image picked into image view.So,instead of using soooo many lines of code,you can just do this

currentRecipe.setImageUri(data.getData());

as data.getData() retursn uri,it will set the image to that and that will also optimize the code.You can use this so that it is done even faster.

Using Glide as mentioned by @CommonsWare will also take a bout 1 or 2 seconds as it search the file and then load.So use currentRecipe.setImageUri(data.getData());.

NOTE: Please give this answer an upvote and mark as an accepted answer if it helps.This will help others too to find the answer quickly.

CodePudding user response:

The reason for the crash seems fairly well explained regarding AddNewRecipe:

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

You cannot have index 1 on an array with length 1, as there is only index 0.

  • Related