繁体   English   中英

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

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

一旦我从图库中选择了一个图像,应用程序就会直接崩溃并在控制台中给我以下错误。 我尝试了很多解决方案,但没有一个成功。 我希望你能帮助我。 非常感谢。

错误信息

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)

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

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

我看到您正在使用该代码将选择的图像加载到图像视图中。因此,您可以这样做,而不是使用太多行代码

currentRecipe.setImageUri(data.getData());

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

使用@CommonsWare 提到的 Glide 搜索文件然后加载时也需要大约 1 或 2 秒。所以使用currentRecipe.setImageUri(data.getData()); .

注意:如果有帮助,请给这个答案点赞并标记为已接受的答案。这也将帮助其他人快速找到答案。

关于AddNewRecipe ,崩溃的原因似乎得到了很好的解释:

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

长度为 1 的数组不能有索引 1,因为只有索引 0。

暂无
暂无

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

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