Reputation: 727
i am able to upload video through this code till 4.1.2 version of android. but in 4.3 this fails.
calling shareVideo() method android displays a list of apps, but in case of selecting youtube from list, youtube app opens then stops without any message and in case of selecting instagram, instagram app crashes. i am not able to upload video through any App including facebook also.
Please tell me what is the issue??
Thanks in advance.
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) {
}
}
});
}
Upvotes: 2
Views: 3962
Reputation: 727
After 4.1 android changed the mechanism of sd card path. now i can upload the the video by getting uri through the id of video file. here is my code
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;
}
and on click of your upload button use this method-
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) {
}
}
}
});
}
Upvotes: 2