Uri.getPath在将文件转换为字节数组时出现错误,位于Android Java项目中。

huangapple 未分类评论49阅读模式
英文:

Uri.getPath get error path when convert file to byte array in Andorid Java project

问题

我想将文件转换为字节数组。uri.getPath 得到的是 /document/image:6139,但完整路径是 /internalStorage/DCIM/Screenshot_2020505.jpg。我想转换的不仅仅是图片(还有 PDF 和 docx 文件)。我做错了什么?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == IMG_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null && getActivity() != null) {
        Uri uri = data.getData();
        try {
            Cursor cursor = null;
            try {
                cursor = getActivity().getContentResolver().query(uri, new String[]{
                        MediaStore.Images.ImageColumns.DISPLAY_NAME
                }, null, null, null);

                if (cursor != null && cursor.moveToFirst()) {

                    String path = uri.getPath();
                    File file = new File(getPath(uri));
                    int size = (int) file.length();
                    byte[] bytes = new byte[size];
                    try {
                        BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
                        buf.read(bytes, 0, bytes.length);
                        buf.close();
                        bytesArray.add(bytes);
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
            TaskReplyAttachmentsAdapter adapter = new TaskReplyAttachmentsAdapter(getContext());
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

变量:private static final int IMG_REQUEST = 777;

英文:

I want convert file to byte array. uri.getPath get /document/image:6139 but full path is /internalStorage/DCIM/Screenshot_2020505.jpg. I want convert not just images(also PDF and docx files). What am I doing wrong?

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == IMG_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null && getActivity() != null) {
            Uri uri = data.getData();
            try {
                Cursor cursor = null;
                try {
                    cursor = getActivity().getContentResolver().query(uri, new String[]{
                            MediaStore.Images.ImageColumns.DISPLAY_NAME
                    }, null, null, null);

                    if (cursor != null && cursor.moveToFirst()) {

                        String path = uri.getPath();
                        File file = new File(getPath(uri));
                        int size = (int) file.length();
                        byte[] bytes = new byte[size];
                        try {
                            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
                            buf.read(bytes, 0, bytes.length);
                            buf.close();
                            bytesArray.add(bytes);
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                } finally {
                    if (cursor != null)
                        cursor.close();
                }
                TaskReplyAttachmentsAdapter adapter = new TaskReplyAttachmentsAdapter(getContext());
                recyclerView.setVisibility(View.VISIBLE);
                recyclerView.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }```

VARAIBLE: ```private static final int IMG_REQUEST = 777;```

</details>


huangapple
  • 本文由 发表于 2020年5月5日 16:40:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61609099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定