如何读取保存在设备内部存储器中的PDF文件?

3
我正在使用以下代码从设备内部存储器下载并阅读PDF文件。
我已成功将文件下载到如下目录:
data/data/packagename/app_books/file.pdf

但是我无法使用像Adobe Reader这样的PDF阅读器应用程序来读取该文件。

下载文件的代码

//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;

读取文件的代码

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();
}

一切都运行正常,但读取器应用显示:"文件路径无效"


试一下这个:Uri uri = Uri.fromFile(fileToRead); - undefined
不,那也不起作用。 - undefined
希望你已经得到了答案。如果没有,请尝试访问以下链接:https://dev59.com/D23Xa4cB1Zd3GeqPcDQ2 - undefined
4个回答

1

你的路径仅适用于你的应用程序。将文件放置在其他应用程序可以“看到”的位置。使用GetExternalFilesDir()或getExternalStorageDirectory()。


我想将其保存在内部存储中,出于特定的原因。不管怎样,感谢您的帮助。 - undefined

0

如何通过创建文件夹将PDF文件从资源文件夹下载到存储空间

确保您已经获得了存储权限,例如支持棉花糖设备等,然后按照以下步骤操作

private void CopyReadAssets()
{
    AssetManager assetManager = getContext().getAssets();

    FileInputStream in = null;

    FileOutputStream out = null;
    File sdcard = Environment.getExternalStorageDirectory();
    File dir = new File(Environment.getExternalStorageDirectory()+File.separator+ "A_level");
    File dir2;
    if (dir.exists() && dir.isDirectory()){
         
        Log.e("tag out", ""+ dir);
    }else {
        dir.mkdir();
        Log.e("tag out", "not exist");
    }

    File file = new File(dir, mTitle+".pdf");

    try
    {
        Log.e("tag out", ""+ file);
       out = new FileOutputStream(file);
        in = new FileInputStream (new File(mPath));

        Log.e("tag In", ""+ in);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e)
    {
        Log.e("tag out", ""+ out);
        Log.e("tag In", ""+ in);
        Log.e("tag", e.getMessage());
        Log.e("tag", ""+file);
        Log.i("tag",""+sdcard.getAbsolutePath() + "A_level");
    }

}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, read);
    }
}

大家来看看这个 - undefined

0

如果您想保持文件的特定于应用程序,您可以使用适用于Lollipop及以上版本的PdfRenderer。谷歌和YouTube上有很多很好的教程。您正在使用的方法是一种安全的方式来存储仅可从应用程序内部阅读的PDF文件。没有像Adobe PDF Reader这样的外部应用程序甚至能够看到该文件。我花了很多时间搜索,但通过使用这个网站和尤其是YouTube,我找到了解决方案。


0

关于在由Context.getDir(String name, int mode)创建的目录中创建的文件的注意事项是,它们只能被您自己的应用程序访问;您只能设置整个目录的模式,而不能设置单个文件的模式。

因此,您可以使用Context.openFileOutput(String name, int mode)。我正在重复使用您的代码作为示例:

try {
    // Now we use Context.MODE_WORLD_READABLE for this file
    FileOutputStream fos = openFileOutput(outputFileName,
            Context.MODE_WORLD_READABLE);

    // Download data and store it to `fos`
    // ...

你可能想看一下这个指南:使用内部存储

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接