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

}

}

}

}

但它无法打开文件位置.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值