I am pretty new to android studio(and java) and i am attempting to make a video processing application. I want the program to upload a video from the users gallery, and then take a picture of the video every second and save it into an array. Logically the code I have written makes sense to me and there are no errors, but when I run the app on my Samsung tablet it crashes after taking the video. I think the setDataSource is unable to find the correct data source, but im not sure how to pass the data from the video along. Can anyone help?
Heres my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedimage = (ImageView) findViewById(R.id.imageView);
}
public void activity(View view) {
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Uri videouri = data.getData();
retriever.setDataSource(String.valueOf(videouri));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInmillisec = Long.parseLong(time);
long seconds = timeInmillisec / 1000;
ArrayList<Bitmap> mylist = new ArrayList<Bitmap>();
for (int i = 0; i < seconds; i++) {
Bitmap bArray = retriever.getFrameAtTime(100000 * i, MediaMetadataRetriever.OPTION_CLOSEST);
mylist.add(bArray);
}
}
Since your videouri
is a Uri
, try calling void setDataSource (Context context, Uri uri)
instead of void setDataSource (String path)
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check for resultCode & requestCode
if (resultCode == RESULT_OK) {
if (requestCode == GET_FROM_GALLERY) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Uri videouri = data.getData();
// "videouri" is Uri, call setDataSource (Context context, Uri uri) instead
retriever.setDataSource(this, videouri);
.....
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.