2016-04-27 5 views
3

Whatsappに送信する予定のビデオファイルにURIを形成する正しい方法は何ですか?私は、次のコードを使用してInstagramのとのWhatsAppに動画を送信しようとしてきた

private void createShareIntent(String type, shareVectorList svList){ 

    // Create the new Intent using the 'Send' action. 
    Intent share = new Intent(Intent.ACTION_SEND); 

    // Set the MIME type 
    share.setType(type); 

    // Create the URI from the media 
    File media = new File(pathName); 
    Uri uri = Uri.fromFile(media); //This is the part I'm confused about 

    // Add the URI to the Intent. 
    share.putExtra(Intent.EXTRA_STREAM, uri); 

    switch(svList) 
    { 
     case INSTAGRAM: 
      share.setPackage("com.instagram.android"); 
      break; 
     case WHATSAPP: 
      share.setPackage("com.whatsapp"); 
      break; 
     default: 
      break; 
    } 

    // Broadcast the Intent. 
    startActivity(Intent.createChooser(share, "Share to")); 
} 

私はInstagramのにビデオを送信すると、それが正常にアップロードします。しかし、Whatsappにビデオを送信すると、ビデオ編集部分があるはずの黒い画面が表示されます。

Androidのログを見ると、FileNotFoundExceptionと表示されます。

誰かが、不正な形式のURI、または存在しないファイルを指しているURIが原因だと言いました。

私はUri.parse("file://" + pathName)を使用しようとしましたが、それでも同じ例外が表示されます。

私は次のコードでContentResolverのクエリからURIを取得してみました:それはちょうど私content://media/external/images/media/<video_file_id>を返し、同じ例外がポップアップしかし

public Uri retrieveUriFromVideoPath(String pathName) { 
    File file = new File(pathName); 
    Cursor cursor = getContentResolver().query(
      MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
      new String[] { MediaStore.Video.Media._ID }, 
      MediaStore.Video.Media.DATA + "=? ", 
      new String[] { pathName }, null); 
    if (cursor != null && cursor.moveToFirst()) { 
     int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID)); 
     cursor.close(); 
     return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id); 
    } else { 
     if (file.exists()) { 
      ContentValues values = new ContentValues(); 
      values.put(MediaStore.Video.Media.DATA, pathName); 
      return getContentResolver().insert(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); 
     } else { 
      return null; 
     } 
    } 
} 

私の質問:

  1. 私はのWhatsAppに共有するつもりのビデオを参照するURIを形成すべきでは?
  2. そのビデオが私が生成するカスタムフォルダにある場合、同じ方法で動作しますか?
  3. ファイル名に空白が含まれる場合はどうなりますか?

答えて

0

代わりに、FileProviderを使用していたトリックを思い出してください。

私はマニフェストに次を追加しました:

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.example.thisFileProvider" 
    android:grantUriPermissions="true" 
    android:exported="false"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/path_to_this_app_directory_permanent" /> 
</provider> 

とURIのために私はこれを行う必要があるでしょう:Uri uri = FileProvider.getUriForFile(this, "com.example.thisFileProvider", media);

私は率直に言って、それが数字にこの長い連れて行ってくれた信じることができませんそれを出す。

関連する問題