我一直在尝试使用下面的代码发送到Instagram的和WhatsApp的视频:将URI发送到我打算发送给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:
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;
}
}
}
但是它只是返回我content://media/external/images/media/<video_file_id>
,同样异常弹出。
我的问题:
- 我应该如何形成是指视频我打算分享的WhatsApp的URI?
- 如果该视频位于我生成的自定义文件夹中,那么同样的方法是否可行?
- 如果文件名包含空格怎么办?