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

问题描述 投票:0回答:3

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

我尝试实现这样的事情:

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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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@[email protected]@[email protected]) 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)
android android-intent download android-manifest
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.