如何从库中检测照片或视频选择

时间:2013-06-03 14:52:43

标签: java android

我使用以下代码从图库中获取图片和视频,但无法从图书馆检测照片或视频

Intent intent = new Intent();
        intent.setType("video/*,image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, SELECT_VIDEO);

1 个答案:

答案 0 :(得分:0)

您是否使用 onActivityResult 来获取所选图片?代码如下。

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
   super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

   switch(requestCode) {
   case 0:
       if(resultCode == RESULT_OK){
           Uri selectedImage = imageReturnedIntent.getData();
           String[] filePathColumn = {MediaStore.Images.Media.DATA};

           Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
           cursor.moveToFirst();

           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String filePath = cursor.getString(columnIndex); // file path of selected image
           cursor.close();
                   //  Convert file path into bitmap image using below line.
           Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

                   // put  bitmapimage in your imageview
            yourimgView.setImageBitmap(yourSelectedImage);
       }
   }
}

对于意图,你可以试试这个。

 final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                        galleryIntent.setType("*/*");
                        startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);