0
我嘗試創建一個應用程序,可以從相機捕獲圖像並根據圖像路徑顯示結果。內,其處理捕獲圖像,如下捕獲圖像後的更新庫android
btnImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory() + File.separator + "Nama_foto_" + waktu + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("return-data", true);
getParentFragment().startActivityForResult(intent, CAPTURE_IMAGE);
}catch (ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
});
而作爲從相機捕捉下面代碼
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == CAPTURE_IMAGE) {
performCrop();
}
else if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
picturePath = file.getAbsolutePath();
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thePic.compress(Bitmap.CompressFormat.JPEG, 80, bytes);
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
out.write(bytes.toByteArray());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//retrieve a reference to the ImageView
//ImageView picView = (ImageView)findViewById(R.id.picture);
//display the returned cropped image
imgView.setImageBitmap(thePic);
}
try {
}catch (Exception e) {
Toast.makeText(getActivity(), "Maaf gagal mengambil foto.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
聲明OnCreateView外部作爲一個空字符串變量picturePath林書面respon的按鈕。 問題是,每當我運行應用程序並捕獲圖像。圖像沒有立即顯示在畫廊中,而是我需要重新運行應用程序(通過android studio)在圖庫中查看圖像。看起來像oncreateview內的東西觸發事件。但我仍然無法弄清楚什麼。
請幫我解決這個問題。由於
我改變了我的問題,,,我應該在哪裏申報上面的代碼? – BrenDonie
試過我們的代碼stil沒有顯示/保存在Android圖庫。但在我的應用程序中顯示圖像 – BrenDonie
哦,把它放在onActivityResult。好的工作....非常感謝你:) – BrenDonie