2011-03-15 93 views
125

我想從圖庫中創建圖片選擇器。我使用代碼android從圖庫中挑選圖片

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

我的問題是,在這個活動和視頻文件顯示。有沒有辦法過濾顯示的文件,以便在此活動中不顯示視頻文件?

+3

本文很好地介紹瞭如何選擇從畫廊圖像:http://androidbitmaps.blogspot.com/2015/04/loading-images-in-android- part-iii-pick.html – 2015-04-17 05:13:03

+0

還有一個和你類似的問題。 http://stackoverflow.com/a/31382240/1835650 – TeeTracker 2015-07-13 11:39:45

回答

231

絕對。試試這個:

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

也不要忘記創建不斷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); 
+3

我在這個「Android系統」,「文檔」上得到2個選項。 如果我選擇Android系統,那麼它會向我展示圖庫和照片。 如何擺脫這個中間選項列表? – Uday 2015-12-11 07:54:38

+3

@Uday這只是因爲你沒有設置默認值。只要離開:) – 2016-01-08 22:31:25

+12

PICK_IMAGE常量必須設置爲什麼?它說「無法解析符號」PICK_IMAGE「 – Michael 2016-08-20 18:15:06

157

有時候,你不能從你選擇的圖片文件。 這是因爲選擇的來自Google+,Drive,Dropbox或任何其他提供商。

最好的解決方案是讓系統通過Intent.ACTION_GET_CONTENT選擇一個內容,並獲得一個內容提供者的結果。

您可以按照下面的代碼或看看我的updated gist

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... 
    } 
} 
+11

if(resultCode == Activity.RESULT_OK){...}'可以使用檢測成功/取消 – 2014-02-04 13:57:03

+0

如何獲取路徑? – delive 2015-10-09 10:27:07

+0

@delive,我認爲你可以嘗試'新的File(data.getData())。getAbsolutePath()'只是一個猜測,我沒有嘗試過 – 2015-12-17 15:14:12

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


} 
+0

謝謝,但ID不適用於API 25?! – 2017-06-12 10:32:46

+0

非常感謝。這個答案幫助我稍後在 – 2017-08-21 01:25:25

+0

上傳圖像我想你的意思是情況1:而不是情況-1:在OnActivityResult中進行切換。 – Mathias 2017-11-26 09:21:43

-5

U可以比這做的答案很容易:

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

我這樣做,它的工作。如果你需要使用文件例如發送到服務器你必須使用其他方式,但只是爲了加載圖像到ImageView你可以做這個簡單的解決方案。 – 2015-10-29 20:52:12

+4

歡迎來到SO!您也可以修改自己的帖子,而不是對自己的帖子發表評論。如果你的評論應該解釋答案,那麼爲什麼不把它放在答案本身呢?就我個人而言,我看不出你對這個老問題的回答(最近)與這個問題有關。我可以建議專注於首先回答問題,但尚未得到公認的答案嗎? – cfi 2015-10-29 21:23:15

+0

我找不到android問題。我怎麼才能看到只是Android的問題? – 2015-10-31 01:53:45

10

您可以使用此方法從圖庫中選擇圖片。只會顯示圖像。

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"); 
      } 
     } 
    } 
+1

@Abhishek ...您在哪裏找到ACTION_PICKT意圖允許的額外列表?謝謝! – 2016-05-27 02:09:09

+0

嘗試了很多方法,但其驚人的大拇指工作很酷 – Adiii 2016-06-22 11:00:27

+1

請不要忘記爲api級別23和更高級別的READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE添加運行時權限。否則,您可能無法從庫中獲取位圖爲非空。請參閱https://stackoverflow.com/a/35285667/3341089 – oguzhan 2018-02-06 21:35:13