2015-04-22 66 views
4

我要求通過代碼的訪問畫廊作爲一名傾聽這裏的用戶:的Android:如何從圖庫中選擇的照片設置爲位圖

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
photoPickerIntent.setType("image/*"); 
startActivityForResult(photoPickerIntent, SELECT_PHOTO); 

不過,我很困惑,如何我會爲選定的照片設置一個變量。

我將代碼設置爲選擇照片的變量的位置?

謝謝:)

+0

你能得到圖像路徑嗎? –

回答

8

你可以這樣做。

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

    // Here we need to check if the activity that was triggers was the Image Gallery. 
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value. 
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked. 
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) { 
     // Let's read picked image data - its URI 
     Uri pickedImage = data.getData(); 
     // Let's read picked image path using content resolver 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); 
     cursor.moveToFirst(); 
     String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); 

     // Do something with the bitmap 


     // At the end remember to close the cursor or you will end with the RuntimeException! 
     cursor.close(); 
    } 
} 
+0

但我應該把這個放在哪裏?只是在onCreate或我應該把它放在聽衆的按鈕來選擇它? – Kinoscorpia

+0

查看編輯:) –

+0

如果我想保存它創建的bitmp,我可以在類的開始創建一個變量稱爲位圖,然後只是說「位圖= BitmapFactory.decodeFile(imagePath,選項);」? – Kinoscorpia

9

首先,你必須覆蓋onActivityResult來獲取文件所選圖像

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (requestCode == SELECT_PHOTO) { 

     if (resultCode == RESULT_OK) { 
      if (intent != null) { 
       // Get the URI of the selected file 
       final Uri uri = intent.getData(); 
       useImage(uri);     
       } 
     } 
     super.onActivityResult(requestCode, resultCode, intent); 

    } 
} 

的URI然後定義useImage(Uri)使用的圖像

void useImage(Uri uri) 
{ 
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 
//use the bitmap as you like 
imageView.setImageBitmap(bitmap); 
} 
1

如果你想顯示的選擇圖像到任何特定的ImageView。 假設我們有RC_PHOTO_PICKER = 1,則這些代碼行應該做的魔術

private void openPhotoPicker() { 
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    photoPickerIntent.setType("image/*"); 
    photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false); 
    startActivityForResult(Intent.createChooser(photoPickerIntent,"Complete Action Using"), RC_PHOTO_PICKER); 
} 

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

    if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK && data != null) { 
     Uri pickedImage = data.getData(); 
     //set the selected image to ImageView 
     mImageView.setImageURI(pickedImage); 
    } 
} 

而且只需調用openPhotoPicker()方法之後

1

替代的阿卡什Kurian說何塞答案

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 

我始終使用

fun getBitmap(file: Uri, cr: ContentResolver): Bitmap?{ 
      var bitmap: Bitmap ?= null 
      try { 
       val inputStream = cr.openInputStream(file) 
       bitmap = BitmapFactory.decodeStream(inputStream) 
       // close stream 
       try { 
        inputStream.close() 
       } catch (e: IOException) { 
        e.printStackTrace() 
       } 

      }catch (e: FileNotFoundException){} 
      return bitmap 
     } 

它同時適用從畫廊的照片和照相機的照片。

更大的問題:使用這種方法Picasso unable to display image from Gallery

開畫廊:

private void openGallery(){ 
    if (Build.VERSION.SDK_INT <19){ 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED); 
    } else { 
     Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("image/*"); 
     startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED); 
    } 
} 

那你就能夠讀取轉換烏里位圖使用afromentioned ContentResolver.openInputStream或設置圖片ImageView.setImageUri(Uri)

相關問題