gpt4 book ai didi

android - BroadcastReceiver 和下载后打开文件

转载 作者:行者123 更新时间:2023-11-29 02:27:11 24 4
gpt4 key购买 nike

我有一个项目需要正常下载文件,然后打开(通常在 PDF 阅读器中)。

我有一个广播接收器类,看起来像这样

public class DownloadBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
{
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor cursor = manager.query(query);
if (cursor.moveToFirst()) {
if (cursor.getCount() > 0) {

int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
Long download_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);
String downloadFilePath = null;
String downloadFileLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File file = new File(Uri.parse(downloadFileLocalUri).getPath());
// status contain Download Status
// download_id contain current download reference id

if (status == DownloadManager.STATUS_SUCCESSFUL) {
String fname=cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));
File pdfFile = new File(Environment.getExternalStorageDirectory()+"/Downloads/"+fname);//File path
if (pdfFile.isFile()) //Checking if the file exists or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(objIntent);//Starting the pdf viewer
} else {
Log.d("OO",Environment.getExternalStorageDirectory()+"/Downloads/"+fname);
Log.d("OO",fname);
// Toast.makeText(getApplicationContext(),"Test",Toast.LENGTH_LONG).show();

}
}
}
}
cursor.close();
}
}

}

list 看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.org.bridgewaterha.boardapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".pdf"></activity>
<receiver android:name=".DownloadBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>

</manifest>

路径文件如下

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="Download" path="."/>
<cache-path name="cache" path="/" />
</paths>

我遇到的问题是在广播接收器类中,行

if (pdfFile.isFile())

总是返回 false。

我已经尝试了一些我在网络上遇到的变体,但似乎都不起作用。

非常感谢任何提示。

最佳答案

下载后您必须检查您的文件是否为 mFile.exists()...

用这个打开pdf文件:

I think your application default behavior is not opening it directly. so we need to use chooser

Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(file));
Intent j = Intent.createChooser(myIntent, "Choose an application to open with:");
startActivity(j);

   while (cursor.moveToNext()) {
if (cursor.getCount() > 0) {
Log.e("filelistingloop","loopfollowingss "+cursor.getCount());
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String file = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if (file != null) {
File mFile = new File(Uri.parse(file).getPath());
final String fileName = mFile.getAbsolutePath().substring(mFile.getAbsolutePath().lastIndexOf('/') + 1, mFile.getAbsolutePath().length());
if (mFile.exists()){
Toast.makeText(yourActivit.this," File donwloaded successfully",Toast.LENGTH_LONG).show();
//here you can open your pdf
}
}
// So something here on success
}else if (status == DownloadManager.STATUS_FAILED){
int message = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
// So something here on failed.
Toast.makeText(getActivity(),"File Not donwloaded "+ message,Toast.LENGTH_LONG).show();
}
}
}

关于android - BroadcastReceiver 和下载后打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com