2014-03-26 42 views
0

我正在編寫一個Android應用程序,使用共享意圖將sdcard中的mp4格式視頻上傳到youtube。當我安裝到手機上時,它第一次完美運行。但是,重新啓動應用後,我無法再分享它。如果我分享到whatsapp,「共享失敗」apear。如果我點擊youtube,我會在logcat上看到「沒有媒體uri(s)」消息。在同一部手機上重新安裝或重新啓動手機無法解決問題。Android共享意圖無法啓動

即使我將應用程序安裝在與其他Android設備相同的代碼中,它也只能運行一次。當我重新啓動應用程序,同樣的問題發生。

我從這個網站代碼: Trouble with youtube upload

這是我的代碼:

findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 

      sharingIntent.setType("video/*"); 

      ContentValues content = new ContentValues(4); 
      content.put(Video.VideoColumns.DATE_ADDED, 
      System.currentTimeMillis()/1000); 
      content.put(Video.Media.MIME_TYPE, "video/mp4"); 
      content.put(MediaStore.Video.Media.DATA, Environment.getExternalStorageDirectory().getAbsolutePath()+"/myVideo/myVideoTemp.mp4"); 
      ContentResolver resolver = getBaseContext().getContentResolver(); 
      Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content); 


      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,""); 
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,""); 
      sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,""); 
      sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri); 


      startActivity(Intent.createChooser(sharingIntent,"Share video")); 
     }   

    }); 

附:有紅顏色沒有錯誤的logcat的

問題將sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

+0

你試圖與分析[android.intentintercept]這個問題(https://play.google.com/store/apps/details?id=uk.co.ashtonbrsc。 android.intentintercept)與您的程序並將結果與​​其他程序的上傳進行比較? – k3b

回答

1

後問題請嘗試以下方法調用「共享」的應用程序:

//Use the URI that points to the video file, in my case, the video file is in local storage (sd card) 
Uri fileUri = Uri.fromFile(videoFile); 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
//Use the setDataAndType method to provide the URI and MIME type to the file 
//Please note, you need to use setDataAndType method to make it work 
intent.setDataAndType(fileUri,URLConnection.guessContentTypeFromName(fileUri.toString())); 
startActivity(intent); 

我有一個博客來向您展示如何調用從本地存儲視頻文件的視頻播放器。它可以幫助:

http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player

+0

如果我使用你的代碼,是否需要在android manifest.xml中加入任何特殊的東西? –

+0

我的代碼從SD卡上讀取視頻文件。所以,如果你從SD卡讀取內容,那麼你需要將以下權限添加到Android清單文件中: