Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read PDF file saved to internal storage of device?
    primarykey
    data
    text
    <p>I am using following code to download and read a PDF file from internal storage on device.</p> <p>I am able to download the files successfully to the directory:</p> <pre><code>data/data/packagename/app_books/file.pdf </code></pre> <p>But I am unable to read the file using a PDF reader application like Adobe Reader.</p> <h2>Code to download file</h2> <pre><code>//Creating an internal dir; File mydir = getApplicationContext().getDir("books", Context.MODE_WORLD_READABLE); try { File file = new File(mydir, outputFileName); URL downloadUrl = new URL(url); URLConnection ucon = downloadUrl.openConnection(); ucon.connect(); InputStream is = ucon.getInputStream(); FileOutputStream fos = new FileOutputStream(file); byte data[] = new byte[1024]; int current = 0; while ((current = is.read(data)) != -1) { fos.write(data, 0, current); } is.close(); fos.flush(); fos.close(); isFileDownloaded=true; } catch (IOException e) { e.printStackTrace(); isFileDownloaded = false; System.out.println(outputFileName + " not downloaded"); } if (isFileDownloaded) System.out.println(outputFileName + " downloaded"); return isFileDownloaded; </code></pre> <h2>Code to read the file</h2> <pre><code>PackageManager packageManager = getPackageManager(); Intent testIntent = new Intent(Intent.ACTION_VIEW); testIntent.setType("application/pdf"); List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); try { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File fileToRead = new File( "/data/data/com.example.filedownloader/app_books/Book.pdf"); Uri uri = Uri.fromFile(fileToRead.getAbsoluteFile()); intent.setDataAndType(uri, "application/pdf"); startActivity(intent); } catch (Exception ex) { Log.i(getClass().toString(), ex.toString()); Toast.makeText(MainActivity.this, "Cannot open your selected file, try again later", Toast.LENGTH_SHORT).show(); } </code></pre> <p>All works fine but the reader app says <em>"File Path is not valid"</em>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload