When downloading finished, either an app chooser open that show some app according to file extension of the downloaded file or it opens an app that is selected as default for that file extension when a user clicks on open in snackbar.
I try to achieve something like this:
For pdf files, it shows something like this
For images, it shows something like this
My Code
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
int last_index = url.lastIndexOf("/");
subPath[0] = url.substring(last_index, url.length()); //downloads file name
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath[0]);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (dm != null) {
dm.enqueue(request);
}
showSnackBar(MainTab.this,"Downloading file", false);
}
});
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
showSnackBar(MainTab.this,"Downloading finished", true);
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
public void showSnackBar(Activity activity, String message, boolean bool){
View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_LONG);
if(bool){
snackbar.setAction(R.string.open, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Downloaded File open", Toast.LENGTH_LONG).show();
Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(subPath[0]));
Intent chooserIntent = Intent.createChooser(baseIntent, "Open with");
if (baseIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooserIntent);
}
}
});
}
snackbar.show();
}
Manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/html"/>
<data android:mimeType="text/pdf"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="video/*"/>
<data android:mimeType="image/*"/>
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
</application>
Please help me to sort it out. Thanks, in advance.
Edit:
Modified code according to @Reaz Murshed
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Downloaded File open", Toast.LENGTH_LONG).show();
String extension = MimeTypeMap.getFileExtensionFromUrl(new File(subPath[0]).getPath());
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(subPath[0])), type);
getApplicationContext().startActivity( intent );
}
logcat:
02-10 16:47:38.966 16467-16467/? I/art: Not late-enabling -Xcheck:jni (already on)
02-10 16:47:39.023 16467-16473/? I/art: Debugger is no longer active
02-10 16:47:39.034 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.208 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.240 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.260 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.279 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.300 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.338 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.362 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.392 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.413 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.432 16467-16467/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/biz.coolpage.aashish.browser-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@biz.coolpage.aashish.browser-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
02-10 16:47:39.434 16467-16467/? W/System: ClassLoader referenced unknown path: /data/app/biz.coolpage.aashish.browser-1/lib/x86
02-10 16:47:39.439 16467-16467/? I/InstantRun: starting instant run server: is main process
02-10 16:47:39.524 16467-16467/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-10 16:47:39.700 16467-16505/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 02-10 16:47:39.702 16467:16467 D/ ]
HostConnection::get() New Host Connection established 0xaa95e200, tid 16467
[ 02-10 16:47:39.734 16467:16505 D/ ]
HostConnection::get() New Host Connection established 0xaa95e800, tid 16505
02-10 16:47:39.737 16467-16505/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-10 16:47:39.737 16467-16505/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-10 16:47:39.747 16467-16505/? D/EGL_emulation: eglCreateContext: 0xaa960360: maj 3 min 1 rcv 4
02-10 16:47:39.753 16467-16505/? D/EGL_emulation: eglMakeCurrent: 0xaa960360: ver 3 1 (tinfo 0xaa94ee60)