2013-06-21 46 views
3

我有興趣將圖像視圖設置爲剛剛由相機拍攝的圖像。相機應用程序工作得很好,它將SD卡上的圖像保存在適當的位置,但是當我嘗試加載圖像並將其設置爲圖像視圖時,它保持空白。我沒有使用最近拍攝的圖像的路徑,而是嘗試對現有圖像的路徑進行硬編碼,但是我得到同樣的問題。我檢查了其他線程,但在代碼中看不到任何差異。將位圖設置爲ImageView onActivityResult不顯示任何內容

這是相機功能:

private void takePicture(){ 
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI"+job_num); 
    image_name = username+"_"+date+".png"; 
    File image_file = new File(imagesFolder, image_name); 
    while(image_file.exists()){ 
     image_name = username+"-"+date+"("+ image_count+").png"; 
     image_count+=1; 
     image_file = new File(imagesFolder,image_name); 
    } 
    image_path = imagesFolder+image_name; 
    Uri uriSavedImage = Uri.fromFile(image_file); 
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    int request_code = 100; 
    startActivityForResult(imageIntent, request_code); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
     ImageView thumb = (ImageView) findViewById(R.id.thumbnail); 
     Bitmap bmp = BitmapFactory.decodeFile(image_path); 
     thumb.setImageBitmap(bmp); 
     Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show(); 
    } 
    else 
     Toast.makeText(this,"Error Saving Image", Toast.LENGTH_SHORT).show(); 
} 

最後這裏是ImageView的:

<ImageView 
    android:id="@+id/thumbnail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/camera" 
    android:layout_marginTop="10dp" 
    android:contentDescription="@string/picture_thumbnail"/> 

什麼需要改變?謝謝!

回答

4

試試這個:

final ImageView thumb = (ImageView) findViewById(R.id.thumbnail); 
    thumb.post(new Runnable() { 
     @Override 
     public void run() 
     { 
      Bitmap bmp = BitmapFactory.decodeFile(image_path); 
      thumb.setImageBitmap(bmp); 
      Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

但我不建議你來解碼在UI線程位圖。

here

Android相機應用程序編碼中作爲演員一小位圖傳送到onActivityResult()的返回意圖的照片,下鍵「數據」。以下代碼檢索此圖像並將其顯示在ImageView中。

private void handleSmallCameraPhoto(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    mImageBitmap = (Bitmap) extras.get("data"); 
    mImageView.setImageBitmap(mImageBitmap); 
} 

注:從「數據」這個縮略圖可能會爲一個圖標是好的,但不是很多。處理全尺寸圖像需要更多工作。

祝好。

+0

感謝您的迴應。不幸的是,我的屏幕上沒有任何東西顯示,但圖像仍然成功保存。 –

+0

你確定位圖不是空的嗎?請提供您的「image_path」。 –

+0

在這裏你可以找到所有你需要採取和保存照片通過意圖 - http://developer.android.com/training/camera/photobasics。html –

1

onActivityResult

InputStream stream = null; 
if (bitmap != null) { 
     bitmap.recycle(); 
    } 
    stream = getContentResolver().openInputStream(data.getData()); 
    bitmap = BitmapFactory.decodeStream(stream); 
    stream.close(); 
    thumb.setImageBitmap(bitmap); 

它總是建議循環使用位圖試試這個代碼。 SO1 & Google Dev

編輯 試試這個代碼

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     imageView.setImageBitmap(photo); 
    } 
+0

感謝您的回覆。當我執行代碼時,它崩潰了:stream = getContentResolver()...此外,出於好奇,爲什麼重新循環位圖很重要? –

+0

我過早地在最後一條評論中「輸入」了,對此感到抱歉。 –

+0

@Vayran相應編輯 – MDMalik