【问题标题】:Internal storage reading writing内部存储读写
【发布时间】:2018-05-24 02:01:25
【问题描述】:

setDestinationInExternalFilesDir(this, "/download", "app-debug.apk");为什么这保存在内部存储中?

以及为什么找不到包含 /data/data/com.example.xxx/files/download/app-debug.apk 的已配置根目录

 downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(this, "/download", "app-debug.apk");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);

File imagePath = new File(getFilesDir(), "download");
File newFile = new File(imagePath, "app-debug.apk");
CharSequence text1 = "no file";
Toast toast1 = Toast.makeText(getApplicationContext(), newFile.getAbsolutePath()+"can't find file ", Toast.LENGTH_SHORT);

if(!newFile.exists()){toast1.show(); return false;}
Uri uri2 = FileProvider.getUriForFile(this,"com.example.xxx",newFile);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(uri2);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("application/vnd.android.package-archive");
startActivity(i);

提供者路径

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--<external-path name="external_files" path="."/>-->
<files-path
 name="tlumacz1"
path="download"/>
</paths>

清单

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.xxx"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

mby 内部存储中有外部存储之类的吗?

【问题讨论】:

  • 添加以下代码:if(!newFile.exists()){Toast( ... file does not exist .. ); return;}。请报告。
  • 对于 getFilesDir() 你需要指定一个
  • 我试过这个不行
  • 不清楚您尝试了什么。这总是有效的。没有理由它会失败。显示完整代码。你添加了我建议的代码吗?你当然应该对我的建议做出反应。
  • 我想通过 fileprovider 进入 Android/data/com.app/files/download/file.apk 我该怎么做?我想打开的文件我用 setDestinationInExternalFilesDir(this, "/download", "app-debug.apk");

标签: java android android-intent android-download-manager


【解决方案1】:

getFilesDir() 替换为getExternalFilesDir()

并调整提供者路径。就像你原来的样子。

【讨论】:

  • 未能找到包含 /storage/emulated/0/Android/data/com.example.xxx/files/download/app-debug.apk '' 我把它写成这样:getExternalFilesDir(null) 因为 () 中需要 sth 它没有 toast 就崩溃了:o
  • 应该是path="."
  • 所以现在 ActivityNotFoundException: 没有找到处理 Intent 的 Activity { act=android.intent.action.VIEW typ=application/vnd.android.package-archive flg=0x10000001 }
  • 使用 setDataAndType() 代替。删除标志。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多