android从图库中挑选图片

我想从画廊创build一个图片select器。 我使用代码

intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, TFRequestCodes.GALLERY); 

我的问题是,在这个活动和video文件显示。 有没有办法来过滤显示的文件,以便在这个活动中不会显示video文件?

绝对。 尝试这个:

 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 

别忘了创build常量PICK_IMAGE ,以便您可以识别用户何时从图库返回活动:

 public static final int PICK_IMAGE = 1; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_IMAGE) { //TODO: action } } 

这就是我所说的图像库。 把它放进去,看看它是否适合你。

编辑:

这将启动文档应用程序。 为了让用户也可以使用他们可能安装的任何图库应用程序:

  Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); getIntent.setType("image/*"); Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); pickIntent.setType("image/*"); Intent chooserIntent = Intent.createChooser(getIntent, "Select Image"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); startActivityForResult(chooserIntent, PICK_IMAGE); 

有时,你不能从你select的图片中得到一个文件。 这是因为select的来自Google+,Drive,Dropbox或任何其他提供商。

最好的解决scheme是要求系统通过Intent.ACTION_GET_CONTENTselect一个内容,并获得一个内容提供者的结果。

你可以按照下面的代码或看看我更新的要点 。

 public void pickImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) { if (data == null) { //Display an error return; } InputStream inputStream = context.getContentResolver().openInputStream(data.getData()); //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap... } } 
 public void FromCamera() { Log.i("camera", "startCameraActivity()"); File file = new File(path); Uri outputFileUri = Uri.fromFile(file); Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, 1); } public void FromCard() { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, 2); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 2 && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex); cursor.close(); bitmap = BitmapFactory.decodeFile(picturePath); image.setImageBitmap(bitmap); if (bitmap != null) { ImageView rotate = (ImageView) findViewById(R.id.rotate); } } else { Log.i("SonaSys", "resultCode: " + resultCode); switch (resultCode) { case 0: Log.i("SonaSys", "User cancelled"); break; case -1: onPhotoTaken(); break; } } } protected void onPhotoTaken() { // Log message Log.i("SonaSys", "onPhotoTaken"); taken = true; imgCapFlag = true; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; bitmap = BitmapFactory.decodeFile(path, options); image.setImageBitmap(bitmap); } 

您可以使用此方法从图库中选取图像。 只有图像将被显示。

 public void pickImage() { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("scale", true); intent.putExtra("outputX", 256); intent.putExtra("outputY", 256); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("return-data", true); startActivityForResult(intent, 1); } 

并重写onActivityResult为

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } if (requestCode == 1) { final Bundle extras = data.getExtras(); if (extras != null) { //Get image Bitmap newProfilePic = extras.getParcelable("data"); } } } 

如果您只是在寻找图像和多个select。

看一次https://stackoverflow.com/a/15029515/1136023

这对未来很有帮助。我个人觉得使用MultipleImagePick很棒。

你可以做到这一点比这个答案更容易:

 Uri Selected_Image_Uri = data.getData(); ImageView imageView = (ImageView) findViewById(R.id.loadedimg); imageView.setImageURI(Selected_Image_Uri);