android - 单击通知打开文件?

标签 android

我想在文件下载(使用服务)服务时通过单击通知打开文件。我已经在我的 mainActivity 中传递了“http://railsboxtech.com/singing/song/ishq_wala_instrument.mp3”这个 URL 来下载它。为此,我使用以下代码:

public class DownloadService extends IntentService {
    String urlPath;
    private int result = Activity.RESULT_CANCELED;

    public DownloadService() {
        super("DownloadService");
    }

    // Will be called asynchronously be Android
    @Override
    protected void onHandleIntent(Intent intent) {
        Uri data = intent.getData();
        urlPath = intent.getStringExtra("urlpath");
        String fileName = data.getLastPathSegment();
        File output = new File(Environment.getExternalStorageDirectory(),
                fileName);
        if (output.exists()) {
            output.delete();
        }

        InputStream stream = null;
        FileOutputStream fos = null;
        try {

            URL url = new URL(urlPath);
            stream = url.openConnection().getInputStream();
            InputStreamReader reader = new InputStreamReader(stream);
            fos = new FileOutputStream(output.getPath());
            int next = -1;
            while ((next = reader.read()) != -1) {
                fos.write(next);
            }
            // Successful finished
            result = Activity.RESULT_OK;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Bundle extras = intent.getExtras();
        if (extras != null) {
            Messenger messenger = (Messenger) extras.get("MESSENGER");
            Message msg = Message.obtain();
            msg.arg1 = result;
            msg.obj = output.getAbsolutePath();
            Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("file://"+msg.obj));
            TaskStackBuilder stackBuilder = TaskStackBuilder
            .create(DownloadService.this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(intent1);
            final PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                DownloadService.this)
                .setContentTitle("File is downloaded")
                .setContentText("Isq_wala_love.mp3").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(resultPendingIntent)
                .addAction(R.drawable.ic_launcher, "Call", resultPendingIntent);
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(0, mBuilder.build());

            try {
                messenger.send(msg);

            } catch (android.os.RemoteException e1) {
                Log.w(getClass().getName(), "Exception sending message", e1);
            }
        }
    }
}

但它无法打开文件位置。

最佳答案

设置 Intent DataAndType如下:

intent.setDataAndType(Uri.fromFile(file), "whatEverMIMEType");

关于android - 单击通知打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874468/

相关文章:

android - 无法在没有错误的情况下在android资源文件中插入字符串

android - 使用 mediaelement 在 Android 上流式传输 hls

java - 为 MediaCodec 设置的有效比特率是多少

java - Android 无法生成 View 绑定(bind) com.sun.tools.javac.code.Symbol$CompletionFailure :

android - Android Studio 3.1.3添加新 Activity 时的奇怪行为

android - 自定义键盘无法在 fragment 中工作

android - 生成签名 APK : Errors while building APK Android Studio

Android:应用程序恢复时执行某些操作

java - 无法从 .net Web 服务获取 JSONArray

java - WakefulBroadcastReceiver 无法解析为类型