Android Activity crashing when trying to fire Intent.ACTION_VIEW from within BroadcastReceiver
My activity should download a file and afterwards open it by sending Intent.ACTION_VIEW. The download itself works fine, I can acce开发者_运维知识库ss it afterwards in the Downloads section. If I'm using a DownloadManager.ACTION_VIEW_DOWNLOADS Intent instead of Intent.ACTION_VIEW it also works fine.
But when using Intent.ACTION_VIEW the activity crashes.
public class RESTTestDownloadActivity extends SOFAActivity {
private DownloadManager downloadManager;
private IntentFilter downloadFilter;
private BroadcastReceiver downloadReceiver;
private long downloadID;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle("RESTTestDownloadActivity");
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
TestGetAttachment();
}
private void TestGetAttachment(){
downloadFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://address:port/directory/testGetAttachment"));
downloadID = downloadManager.enqueue(request);
downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(RESTTestDownloadActivity.this, "ACTION_DOWNLOAD_COMPLETE received.", Toast.LENGTH_LONG);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadID);
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
System.out.println("Download matches.");
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
if(status!=DownloadManager.STATUS_SUCCESSFUL){
System.out.println("Download != STATUS_SUCCESSFUL.");
AlertDialogBuilder.setTitle("Error")
.setMessage(reason)
.setPositiveButton("OK", null)
.show();
} else {
System.out.println("Download = STATUS_SUCCESSFUL.");
Toast.makeText(RESTTestDownloadActivity.this, "Download successful.", Toast.LENGTH_LONG).show();
Uri dlUri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
System.out.println("Download-URI: " + dlUri.toString());
unregisterReceiver(downloadReceiver);
downloadReceiver = null;
// This is working perfectly fine
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
// But this is crashing
// Intent i = new Intent(Intent.ACTION_VIEW, dlUri);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(i);
}
downloadManager.remove(downloadID);
downloadReceiver = null;
}
}
};
registerReceiver(downloadReceiver, downloadFilter);
}
@Override
public void onPause(){
super.onPause();
if(downloadReceiver!=null){
System.out.println("Unregistering downloadReceiver...");
unregisterReceiver(downloadReceiver);
}
}
@Override
public void onResume(){
super.onResume();
if(downloadReceiver!=null){
System.out.println("Registering downloadReceiver...");
registerReceiver(downloadReceiver, downloadFilter);
}
}
As you can see I already tried to add the FLAG_ACTIVITY_NEW_TASK as mentioned here.
LogCat does output the following:
08-05 13:04:35.437: INFO/System.out(2449): *.RESTTestDownloadActivity.onResume()
08-05 13:04:35.446: INFO/System.out(2449): Registering downloadReceiver...
08-05 13:04:35.846: INFO/ActivityManager(77): Displayed *.RESTTestDownloadActivity: +561ms
08-05 13:04:36.046: INFO/DownloadManager(274): Initiating request for download 24
08-05 13:04:40.956: DEBUG/dalvikvm(216): GC_EXPLICIT freed 26K, 47% free 3220K/6023K, external 6059K/7285K, paused 68ms
08-05 13:04:45.855: DEBUG/dalvikvm(157): GC_CONCURRENT freed 795K, 56% free 2872K/6471K, external 2402K/2630K, paused 10ms+15ms
08-05 13:04:46.135: INFO/System.out(2449): Download gefunden.
08-05 13:04:46.135: INFO/System.out(2449): Download = STATUS_SUCCESSFUL.
08-05 13:04:46.155: INFO/System.out(2449): Download-URI: content://downloads/my_downloads/24
08-05 13:04:46.165: INFO/ActivityManager(77): Starting: Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 } from pid 2449
08-05 13:04:46.175: DEBUG/AndroidRuntime(2449): Shutting down VM
08-05 13:04:46.185: WARN/dalvikvm(2449): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): FATAL EXCEPTION: main
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE pkg=* (has extras) } in *.RESTTestDownloadActivity$1@405376e8
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.os.Handler.handleCallback(Handler.java:587)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.os.Looper.loop(Looper.java:123)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at dalvik.system.NativeStart.main(Native Method)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 }
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.Activity.startActivityForResult(Activity.java:2827)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.Activity.startActivity(Activity.java:2933)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at ***.RESTTestDownloadActivity$1.onReceive(RESTTestDownloadActivity.java:75)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
08-05 13:04:46.195: ERROR/AndroidRuntime(2449): ... 9 more
08-05 13:04:46.216: WARN/ActivityManager(77): Force finishing activity *.RESTTestDownloadActivity
08-05 13:04:46.739: WARN/ActivityManager(77): Activity pause timeout for HistoryRecord{408abbf0 *.RESTTestDownloadActivity}
Any idea why only Intent.ACTION_VIEW results in a crash? Thanks in advance!
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://downloads/my_downloads/24 flg=0x10000000 }
Please add a MIME type to your Intent
, to help Android find the appropriate activity for you.
Try saving the file on your SD card and then firing an intent with ACTION_VIEW. For this to work, you need to set the intent data with Uri.fromFile(file_object_pointing_to_your_file) method. (Uri.parse() method will cause a crash in this case)
You are getting this error because You are using using DownloadManager.ACTION_VIEW_DOWNLOADS
Intent instead of Intent.ACTION_VIEW
and for Intent.ACTION_VIEW
there is an activity in your app or any other app which can handle your view request.I mean item which you are downloading can not be opened by any activity.
SO you have either create your own activity which can open your item or you have to install external apk which can open your item.
So best choice is that you have to use DownloadManager.ACTION_VIEW_DOWNLOADS
I had a similar android.content.ActivityNotFoundException
. For me, it helped to convert the "content" URI to a "file" URI first:
Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
if ("content".equals(uri.getScheme())) {
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String filePath = cursor.getString(0);
cursor.close();
uri = Uri.fromFile(new File(filePath));
}
if ("file".equals(uri.getScheme()) {
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Required if launching outside of an activity
installIntent.setDataAndType(uri, downloadManager.getMimeTypeForDownloadedFile(downloadId));
startActivity(installIntent);
}
精彩评论