如何更改以下代码导入视频而不是相机/图库中的图像?

时间:2017-10-03 12:19:58

标签: android

以下代码用于导入图像。如何修改它以导入视频?是否有类似于导入/存储视频的位图?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==1 && resultCode==RESULT_OK && data!=null){

        Uri selectedImage=data.getData();

        try{
            Bitmap bitmap=MediaStore.Images.Media.getBitmap((this.getContentResolver()),selectedImage);
            ImageView imageView=(ImageView)findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

为了导入视频,你会像这样修改你的意图

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST);

并像这样访问该视频

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_VIDEO_REQUEST) {
            Uri selectedVideoUri = data.getData();

            String videopath = getPath(selectedVideoUri);
            if (videopath != null) {
              Log("Video Path : ",videopath);
            }
    }
}
 public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

答案 1 :(得分:0)

要导入视频,您可以这样做

   Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, SELECT_VIDEO); 

然后访问它使用类似的东西:

 @ Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_VIDEO) {
            selectedVideoPath = getPath(data.getData());
            try {
                if(selectedVideoPath == null) {
                    Log.e("selected video path = null!");
                    finish();
                } else {
                        /**
                         * try to do something there
                         * selectedVideoPath is path to the selected video
                         */
                }
            } catch (IOException e) {
                //#debug
                e.printStackTrace();
            }
        }
    }
    finish();
}


public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor!=null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}

或者你可以使用一些库:

https://github.com/coomar2841/image-chooser-library