简体   繁体   English

当我从图库中选择图像时应用程序崩溃

[英]App crashes when I select an image from the gallery

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代码 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 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;
                }
        }
    }

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.作为data.getData()返回 uri,它会将图像设置为该图像,这也将优化代码。您可以使用它以便更快地完成。

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());使用@CommonsWare 提到的 Glide 搜索文件然后加载时也需要大约 1 或 2 秒。所以使用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.注意:如果有帮助,请给这个答案点赞并标记为已接受的答案。这也将帮助其他人快速找到答案。

The reason for the crash seems fairly well explained regarding AddNewRecipe :关于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.长度为 1 的数组不能有索引 1,因为只有索引 0。

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

相关问题 当“不”从文件选择/图库中选择图像时,应用程序崩溃 - App crashes when 'not' selecting an image from file choose / gallery 应用在图片选择时崩溃 - App crashes on Image Select 无法从另一个本机应用程序的 Android 库中选择图像 - Unable to select image from Android gallery from another native app android:当我在图库应用中选择图片时,应用消失了 - android : when i select a picture in gallery app, app is gone 无法从图库Android应用中选择图片(显示为灰色) - Unable to select image from gallery android app (grayed out) 当从图库“文档不断停止”中选择图像时,显示错误 - When image select from Gallery 'Documents keeps stopping' error show 从图库中选择图像时未获得正确的路径 - Not getting correct Path when Select Image from Gallery 第三次从图库中选择图像时内存不足 - Out of memory when select image from gallery for the third times Android 从图库中选取图像后,android应用程序崩溃-cursor.getColumnIndex返回-1 - android app crashes after picking a image from gallery - cursor.getColumnIndex returns -1 当我尝试将图像保存到图库时,应用程序不断崩溃 - App keep crashing when I try to save image to gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM