: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;
}
}
}
。
私の質問:
- 私はのWhatsAppに共有するつもりのビデオを参照するURIを形成すべきでは?
- そのビデオが私が生成するカスタムフォルダにある場合、同じ方法で動作しますか?
- ファイル名に空白が含まれる場合はどうなりますか?