从我的Gallery Android Studio中保存的视频中挑选帧

时间:2018-02-07 02:48:32

标签: java android video-capture

我是android studio(和java)的新手,我正在尝试制作视频处理应用程序。我希望程序从用户库中上传视频,然后每秒拍摄一段视频并将其保存到阵列中。从逻辑上讲,我写的代码对我来说很有意义并且没有错误,但是当我在我的三星平板电脑上运行应用程序时,它会在拍摄视频后崩溃。我认为setDataSource无法找到正确的数据源,但我不知道如何从视频中传递数据。有人可以帮忙吗?

继承我的代码:

@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);

    }
}

1 个答案:

答案 0 :(得分:0)

由于您的videouriUri,请尝试拨打void setDataSource (Context context, Uri uri)而不是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);

            .....

    }
}