2

我正在使用以下代码从设备的内部存储中下载和读取 PDF 文件。

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

data/data/packagename/app_books/file.pdf

但我无法使用 Adob​​e 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();
}

一切正常,但阅读器应用程序显示“文件路径无效”

4

4 回答 4

1

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

于 2013-03-14T10:31:18.910 回答
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`
    // ...

您可能需要查看本指南:使用内部存储

于 2013-03-14T19:55:38.140 回答
0

如果您想保持文件应用程序的特定性,您可以使用适用于 Lollipop 及更高版本的 PdfRenderer。google 和 youtube 上有很棒的教程,效果很好。您使用的方法是一种安全的方式来存储只能从应用程序内部读取的 PDF 文件。没有像 Adob​​e PDF Reader 这样的外部应用程序甚至可以看到该文件。我花了很多时间搜索,但我通过使用这个网站,尤其是 youtube,找到了针对我的特定用途的解决方案。

于 2017-09-24T04:12:07.433 回答
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);
    }
}
于 2017-09-13T07:39:36.537 回答