Issue on selection file in android [duplicate]

半腔热情 提交于 2021-01-07 02:39:12

问题


I am adding my source codes. If possible add your source code(File Uploading) to help me.

OnActivityResult :

try {


        // When an file is picked
        if (requestCode == 3 && resultCode == RESULT_OK && null != data) {

            // Get the file from data
//                String path = data.getStringExtra(mediaPath);
            File file = new File(String.valueOf(data.getData()));
            Uri selectedFile = Uri.fromFile(new File(file.getAbsolutePath()));
            String[] filePathColumn = {MediaStore.Files.FileColumns.MEDIA_TYPE};

            Log.d("PATH",String.valueOf(data.getData()));

            Cursor cursor = getContentResolver().query(selectedFile, filePathColumn, null, null, null);
            assert cursor != null;
            cursor.moveToFirst();


            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mediaPath = cursor.getString(columnIndex);
            txt.setText(String.valueOf(data.getData()));
            cursor.close();



//                try {
//                    String parsedText="";
//                    PdfReader reader = new PdfReader(mediaPath);
//                    int n = reader.getNumberOfPages();
//                    for (int i = 0; i <n ; i++) {
//                        parsedText   = parsedText+ PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
//                    }
//                    Log.d("PDF_TEXT",parsedText);
//                    System.out.println(parsedText);
//                    reader.close();
//                } catch (Exception e) {
//                    System.out.println(e);
//                }
            }
            else {
                Toast.makeText(this, "You haven't picked any file", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
            Log.d("EXCEPTION_ERROR",e.toString());
        }

Issue :

    java.lang.AssertionError
    at com.example.pdfreader.MainActivity.onActivityResult(MainActivity.java:78)

MainActivity.java:78 is representing assert cursor != null; I tried Log.d("CURSOR",String.valueOf(cursor)) before assert cursor != null;` Then I get in logcat cursor is null.

I earlier was facing problem also which is fixed. Here's the stack's question

来源:https://stackoverflow.com/questions/65528800/issue-on-selection-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!