0

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;```
Łukasz Cz
  • 11
  • 2

0 Answers0