我有一个关于下载 pdf 文件并使用安装在手机上的 pdf 阅读器应用程序打开它的问题。我是 android 的新手,正在努力提高,但卡在了这部分。
所以我现在有: 我有一个 Activity ,现在开始下载 pdf 文件并尝试打开是有意的。现在一切都是静态的,所以这就是为什么我有一个固定的 url。
private void DownloadFile(){
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("http://awebiste.adomain/afile.pdf");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("My Data Download");
request.setDescription("Android Data download using DownloadManager.");
request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"test.pdf");
Long downloadReference = downloadManager.enqueue(request);
if (downloadReference != null){
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.parse(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf"), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.v("OPEN_FILE_PATH", getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf");
startActivity(target);
} else {
//TODO something went wrong error
}
}
现在文件已下载并保存在/storage.sdcard0/Android/data/<application>/files/download
下的应用程序文件夹中并且可以从文档浏览器中打开文件。但是当我在我的代码按钮上使用 Intent 时,我得到了一个文件无法打开的 toast 。在谷歌上搜索后,我认为这是一个权限问题,因为这些文件对应用程序是私有(private)的。那么如何公开这些文件呢?
最佳答案
这是经过一整天的搜索后我是如何做到的,您的代码帮助我解决了这个问题:
String name;
DownloadManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_interview);
Button down = (Button) findViewById(R.id.download);
down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
down();
}
});
}
public void down() {
mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri)
.setAllowedOverRoaming(false)
.setTitle("Downloading")
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, name + "CV.pdf")
.setDescription("Download in progress").setMimeType("pdf");
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(broadcast, intentFilter);
}
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
Intent testIntent = new Intent("com.adobe.reader");
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
BroadcastReceiver broadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
showPdf();
}
};
关于java - 安卓打开下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335129/