0

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

  • Whats the code in `MainActivity` line number 78? – Atish Agrawal Jan 01 '21 at 10:12
  • @AtishAgrawal `assert cursor != null;` –  Jan 01 '21 at 10:14
  • Most common issues could be Manifest file permissions. If it is not the case, try using `getContentResolver().openInputStream(uri)` to get an InputStream from a URI. – Atish Agrawal Jan 01 '21 at 10:19
  • @AtishAgrawal error from `catch` `java.io.FileNotFoundException: /content:/com.android.providers.downloads.documents/document/3358: open failed: ENOENT (No such file or directory)` –  Jan 01 '21 at 10:26
  • Can you post the entire Catch Exception? – Atish Agrawal Jan 01 '21 at 10:28
  • @AtishAgrawal `01-01 16:28:40.453 24734-24734/com.example.pdfreader D/EXCEPTION_ERROR: java.io.FileNotFoundException: /content:/com.android.providers.downloads.documents/document/3358: open failed: ENOENT (No such file or directory)` –  Jan 01 '21 at 10:29
  • 1
    Your problem is that you are trying to convert an `Uri` to a `File` (`File file = new File(String.valueOf(data.getData()));`). – HB. Jan 01 '21 at 11:27

0 Answers0