在通知点击打开文件? [英] open file on click of notification?

查看:129
本文介绍了在通知点击打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文件时,它会下载(使用服务)的notification.I已通过点击服务的 http://railsboxtech.com/singing/song/ishq_wala_instrument.mp3 的网址,我mainActivity下载它。对于我使用以下code:

I want to open a file when it gets downloaded(Using Services) services on click of the notification.I have passed "http://railsboxtech.com/singing/song/ishq_wala_instrument.mp3" this url in my mainActivity for downloading it. For that I am using following code:

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);
            }
            // Sucessful 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);
            }

        }
    }


}

但它无法打开文件位置。

But it's not able to open the file location.

推荐答案

设置意图 DataAndType 如下:

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

这篇关于在通知点击打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆