我有興趣將圖像視圖設置爲剛剛由相機拍攝的圖像。相機應用程序工作得很好,它將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"/>
什麼需要改變?謝謝!
感謝您的迴應。不幸的是,我的屏幕上沒有任何東西顯示,但圖像仍然成功保存。 –
你確定位圖不是空的嗎?請提供您的「image_path」。 –
在這裏你可以找到所有你需要採取和保存照片通過意圖 - http://developer.android.com/training/camera/photobasics。html –