gpt4 book ai didi

android - 无法通过 android 4.3 中的 action_send Intent 上传视频

转载 作者:行者123 更新时间:2023-11-29 21:16:20 25 4
gpt4 key购买 nike

我可以通过此代码上传视频,直到 4.1.2 版本的 android。但在 4.3 中,这失败了。

调用 shareVideo() 方法 android 会显示一个应用程序列表,但是如果选择youtube 从列表中,youtube 应用程序打开然后停止,没有任何消息,如果选择 instagram,instagram 应用程序崩溃。我也无法通过包括 Facebook 在内的任何应用程序上传视频。

请告诉我这是什么问题??

提前致谢。

public void shareVideo(View view){
new Handler().post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, filename);

Uri videoURI = getBaseContext().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
//Uri videoURI = Uri.fromFile(new File(filename));
Intent intent = new Intent(Intent.ACTION_SEND);
//intent.setAction(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI);
try {
startActivity(Intent.createChooser(intent,"Upload video via:"));
} catch (android.content.ActivityNotFoundException ex) {

}
}
});

}

最佳答案

android 4.1之后改变了sd卡路径的机制。现在我可以通过视频文件的 id 获取 uri 来上传视频。这是我的代码

public static String getVideoContentUriFromFilePath(Context ctx, String filePath) {

ContentResolver contentResolver = ctx.getContentResolver();
String videoUriStr = null;
long videoId = -1;
Log.d("first log","Loading file " + filePath);

// This returns us content://media/external/videos/media (or something like that)
// I pass in "external" because that's the MediaStore's name for the external
// storage on my device (the other possibility is "internal")
Uri videosUri = MediaStore.Video.Media.getContentUri("external");

Log.d("second log","videosUri = " + videosUri.toString());

String[] projection = {MediaStore.Video.VideoColumns._ID};

// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);

Log.d("third log","Video ID is " + videoId);
cursor.close();
if (videoId != -1 ) videoUriStr = videosUri.toString() + "/" + videoId;
return videoUriStr;
}

点击您的上传按钮使用这个方法-

public void shareVideo(View view){
new Handler().post(new Runnable() {

@Override
public void run() {
String newPath=getVideoContentUriFromFilePath(ShareVideoActivity.this, videoPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
//intent.setAction(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath));
try {
startActivity(Intent.createChooser(intent,"Upload video via:"));
} catch (android.content.ActivityNotFoundException ex) {

}
}
}
});

}

关于android - 无法通过 android 4.3 中的 action_send Intent 上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21334484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com