我使用下面的代碼把一個圖像保存到SD card中,但是如何獲得保存了的圖像的路徑,因為我想使用這個路徑給下一個 activity 中的 ImageView 設置圖像。
我試著使用onActivityResult()
,但是不能獲得路徑。因為如果你想在浏覽器文件夾中打開一個 intent,onActivitySesult()
會被摧毀,但是我不想打開 gallery 或者 intent,就能訪問獲取文件路徑。 請大家幫忙解決這個問題,謝謝。
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
FileOutputStream outStream = null;
try {
// Write to SD Card
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",System.currentTimeMillis()));
outStream.write(arg0);
outStream.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
//Log.e("Photo", "Image files get saved in SD Card only",e);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}};
指定 System.currentTimeMillis() 為 String 或 long,然後傳遞到 File Outputstream 中。
try {
String stored_date=System.currentTimeMillis()+"";
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",stored_date));
outStream.write(arg0);
outStream.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
System.out.println();
} catch (FileNotFoundException e) {}
catch (IOException e) {}