点击通知后如何打开文件?

4

我希望在使用服务(Service)下载文件时,在通知栏点击通知时能够打开该文件。我在主活动(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);
            }
        }
    }
}

但它无法打开文件位置。

1
尝试将意图设置为 intent.setDataAndType(Uri.fromFile(file), "whatEverMIMEType"); - Ahmad Kayyali
使用了!但仍然不起作用。 - TheLittleNaruto
你能在SD卡中实际看到下载的文件吗? - Ahmad Kayyali
当然可以!!但我希望它在通知被点击时打开。 - TheLittleNaruto
实际上,我之前放错了文件 Uri.fromFile(file)。当我更正后,它就可以工作了! - TheLittleNaruto
我很高兴能够帮忙,同时我已经将它发布为答案,这样每个人都可以轻松地看到解决方案。 - Ahmad Kayyali
1个回答

7

将Intent的DataAndType设置如下:

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

如何适当地设置MIME类型?如果是文本、照片等,应该怎么做?"/"只是最后的选择。 - Radu
1
谢谢Ahmad,实际上我使用了MimeTypeMap.getSingletion().getMimeTypeFromExtension(extension)。 - Radu
Radu:获取正确的 MIME 类型是个好主意,感谢您分享。 - Ahmad Kayyali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接