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

Android 如何读取保存到设备内部存储器中的PDF文件?,android,android-intent,storage,internal,Android,Android Intent,Storage,Internal,我使用以下代码从设备的内部存储器下载并读取PDF文件 我能够成功地将文件下载到目录: data/data/packagename/app_books/file.pdf 但我无法使用Adobe reader之类的PDF阅读器应用程序读取文件 下载文件的代码 读取文件的代码 所有操作都正常,但reader应用程序显示“文件路径无效”。您的路径仅对您的应用程序有效。将文件放置在其他应用程序可以“查看”的位置。使用GetExternalFilesDir()或getExternalStorageDire

我使用以下代码从设备的内部存储器下载并读取PDF文件

我能够成功地将文件下载到目录:

data/data/packagename/app_books/file.pdf
但我无法使用Adobe reader之类的PDF阅读器应用程序读取文件

下载文件的代码 读取文件的代码
所有操作都正常,但reader应用程序显示“文件路径无效”。

您的路径仅对您的应用程序有效。将文件放置在其他应用程序可以“查看”的位置。使用GetExternalFilesDir()或getExternalStorageDirectory()。

请注意,在由创建的目录中创建的文件只能由您自己的应用程序访问;您只能设置整个目录的模式,而不能设置单个文件的模式

所以你可以用。我将使用您的代码作为示例:

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`
    // ...
您可能想看一下本指南:。

如何通过制作文件夹将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);
    }
}

如果你想保持文件应用程序的特定性,你可以使用PdfRenderer,用于棒棒糖及以上版本。谷歌和youtube上有很多很好的教程。您使用的方法是一种安全的方式来存储PDF文件,该文件只能从应用程序内部读取。没有像Adobe PDF Reader这样的外部应用程序能够看到该文件。我花了很多时间搜索,但我通过使用该网站,特别是youtube,找到了一个解决我具体使用问题的方法。

试试这个:-Uri=Uri.fromFile(fileToRead);不,那也不行。希望你已经得到答案了。如果不尝试此操作,我希望出于特定原因将其保存在内部存储器中。不管怎样,谢谢你的帮助检查一下这个
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`
    // ...
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);
    }
}