下载完成后如何显示app选择器

时间:2018-02-10 05:40:00

标签: android android-intent download android-manifest

下载完成后,根据下载文件的文件扩展名打开显示某个应用的应用选择器,或者当用户点击小吃栏中的“打开”时,它会打开一个被选为该文件扩展名默认应用的应用。

我尝试实现这样的目标:

For pdf files, it shows something like this 对于pdf文件,它显示类似

的内容

For images, it shows something like this 对于图片,它会显示类似的内容

我的代码

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();
    }

清单文件:

<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>

请帮我解决一下。提前谢谢。

修改

根据@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)

3 个答案:

答案 0 :(得分:1)

在致电startActivity(Intent)之前,您应该检查是否可以处理意图。

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
    Log.d(TAG, "No Intent available to handle action");
}

答案 1 :(得分:0)

据我所知,您在检测文件何时完全下载并且立即获得文件名时没有问题。现在基于上述假设,只要您手中获得完全指定的文件名,就可以轻松选择建议打开这些类型文件的应用程序。

从下载文件的文件扩展名中获取文件类型。我有一个示例代码来演示过滤应用程序选择器的想法。

200|3
200|5
100|2
50|2
50|4

如果我在某些方面误解了,请告诉我。

答案 2 :(得分:0)

我试试这个并且有效。

public void onClick(View v) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(subPath[0]);
    String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

    Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(subPath[0]));
    baseIntent.setType(type);

    if (baseIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(baseIntent);
    }
    else {
        Toast.makeText(getApplicationContext(), "File unable to open", Toast.LENGTH_LONG).show();
    }
}