2016-01-13 76 views
0

我正在使用此代碼從圖庫或拍照中獲取圖像。
拍照完美。
從相機拍攝的照片將在影像上顯示,
但與畫廊拍攝的圖像不同,它總是空白。ImageView不改變圖像從圖庫中獲取

public void fromCamera(int id) { 
    Intent intent = new Intent(
      android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, IMAGE_CAPTURE); 
} 

public void fromGallery(int id) { 
    Intent intent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    startActivityForResult(Intent.createChooser(intent, "Pick a Picture"), 
      IMAGE_PICK); 
} 

public String getRealPathFromURI(Uri contentUri) { 
    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } catch (Exception e) { 
     return contentUri.getPath(); 
    } 
} 

private void imageFromCamera(int resultCode, Intent data) { 
    this.mButtonCarPhoto.setImageBitmap((Bitmap) data.getExtras().get("data")); 
    Uri selectedImageUri = data.getData(); 
    mSelectedImagePath = getRealPathFromURI(selectedImageUri); 
    Log.e("Camera",mSelectedImagePath); 
    // mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath); 
} 

private void imageFromGallery(int resultCode, Intent data) { 
    Uri selectedImage = data.getData(); 
    String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

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

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    mSelectedImagePath = cursor.getString(columnIndex); 
    Log.e("Gallery",mSelectedImagePath); 
    cursor.close(); 
    this.mButtonCarPhoto.setImageBitmap(BitmapFactory.decodeFile(mSelectedImagePath)); 
    //mBase64Image = CONVERT_IMG_BASE64(mSelectedImagePath);*/ 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     switch (requestCode) { 
      case IMAGE_PICK: 
       this.imageFromGallery(resultCode, data); 
       break; 
      case IMAGE_CAPTURE: 
       this.imageFromCamera(resultCode, data); 
       break; 
      default: 
       break; 
     } 
    } 
} 

我在做什麼錯了?

回答

0

爲什麼這樣?

intent.setType("image/*"); 
startActivityForResult(Intent.createChooser(intent, "Pick a Picture"), IMAGE_PICK); 

如果您只是想從圖庫中選擇,請從您的代碼中刪除。

0

試試這個,它完美對我來說:

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

,然後覆蓋onActivityResult方法:

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

    if(requestCode==1) { // from gallery 
     if (data != null) { 
      Uri photoUri = data.getData(); 
      if (photoUri != null) { 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       path = cursor.getString(columnIndex); // path is a static variable 
       cursor.close(); 
       new GetImages().execute(); 
      } 
     } 
    } 
} 

,這是內部類GetImages,在一個異步任務更新圖像視圖:

public class GetImages extends AsyncTask<Void, Void, JSONObject> 
{ 
    @Override 
    protected JSONObject doInBackground(Void... params) 
    { 
     Bitmap bitmap = BitmapFactory.decodeFile(path.toString().trim()); 
     if (bitmap != null) { 
      bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, true); 
      currentBitmap = bitmap; // current Bitmap is a static variable 
      drawable = new BitmapDrawable(bitmap); // drawable is a static variable 
      // salvataggio foto in locale 
      db.open(); 
      String bitmapString = BitMapToString(bitmap); 
      db.updateImage(bitmapString, userName); 
      db.close(); 
      JSONObject json = savePhotoInRemote(bitmapString); 
      return json; 
     } 
     return null; 
    } 

    protected void onPostExecute(JSONObject result) 
    { 
     imageview_photo.setBackground(drawable); // QUI AGGIORNI LA TUA IMAGE VIEW 
    } 
}