2013-02-14 64 views
4

我想打開一個文件,當它被下載(使用服務)服務點擊通知。我在我的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(文件), 「whatEverMIMEType」);使用' – 2013-02-14 12:11:47

+0

!但仍然努力工作。 – TheLittleNaruto 2013-02-14 12:21:02

+0

你真的能看到下載到SD卡中的文件嗎? – 2013-02-14 12:22:08

回答

7

設置意圖DataAndType如下:

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

如何設置適當的MIME類型?如果是文字,圖片等? 「*/*」只是最後一個選項。 – Radu 2013-11-26 14:40:20

+0

http://developer.android.com/training/sharing/receive.html – 2013-11-26 16:27:57

+1

謝謝艾哈邁德,其實我用MimeTypeMap.getSingletion()。getMimeTypeFromExtension(extension) – Radu 2013-11-27 09:43:37

相關問題