【问题标题】:Internal Storage "the document path is not valid"内部存储“文档路径无效”
【发布时间】:2013-10-21 07:25:11
【问题描述】:

错误:文档路径无效

我想将 pdf 存储在内部存储中并在我想阅读它之后。

我在这个网站上看到了很多问题,但没有一个能帮助我。

我在 Environment.getExternalStorageDirectory() 中的代码工作文件现在我使用 getCacheDir() 进行内部存储。

用于编写 pdf

 FileOutputStream fos = new FileOutputStream(file);
                fos.write(baf.toByteArray());
                fos.close();

文件写入完成,可以看到缓存文件夹中的文件了。

用于阅读 pdf

file = new File(getCacheDir()+ File.separator+fileName);

PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (list.size() > 0 && file.isFile()) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/pdf");
                startActivity(intent);
            }

我收到错误“文档路径无效”

【问题讨论】:

  • 您正在尝试将一个应用程序的内部存储(缓存目录)与其他应用程序共享,这在 Android 中是不允许的。

标签: android


【解决方案1】:

使用getFilesDir () 进行内部存储

公共抽象文件getFilesDir()

在 API 级别 1 中添加 返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。

返回 返回包含应用程序文件的目录的路径。 也可以看看 打开文件输出(字符串,整数) 获取文件流路径(字符串) getDir(String, int)

例如

File yourFile = context.getFilesDir() + "/" + "file_name";

详情请关注here

【讨论】:

猜你喜欢
  • 2012-01-22
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多