我寫了一個android代碼來拍照(使用android相機應用程序)。我想將此照片保存在外部存儲器中,因此我使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)來獲取目錄路徑。獲取外部存儲路徑在Android 4.1.1中不起作用
這是我的代碼:
public void takePhoto(View b){
/** Esta función recoge la pulsación del botón de tomar fotografía.
* Se encarga de lanzar la cámara de fotos del móvil y permitir al
* usario hacer una fotografía.
*
* Se comprueba si se puede almacenar la foto, para después enviarla
* al backend. Si no, no se permite hacer la foto.
*/
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Se crea un File con el path donde se quiere almacenar la foto
fileName = new String("Incidencia_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".jpg");
storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/SmartRoutes", fileName);
//storageDir = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/SmartRoutes", fileName);
if(storageDir != null)
Log.d(logTag, "Path: "+storageDir.toString());
// y se pasa al intent como extra.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(storageDir));
startActivityForResult(takePictureIntent, TAKE_PHOTO_CODE);
}
else
Toast.makeText(this, "No es posible sacar una fotografía", Toast.LENGTH_SHORT).show();
}
然後在onActivityResult(),我試圖讓存儲在 「storageDir」 的照片,並簡單地打印在logcat的路徑:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/** En esta función se recogen los resultados de las activities lanzadas desde esta.
* En este caso la de la cámara de fotos.
*/
switch (requestCode) {
case TAKE_PHOTO_CODE:
if(resultCode == RESULT_OK){
Toast.makeText(this, "Foto tomada correctamente", Toast.LENGTH_SHORT).show();
// Se añade la foto a la galería
//Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//photoFile = new File(storageDir.getAbsolutePath());
photoFile = storageDir;
Toast.makeText(this, "File.toString(): "+photoFile.toString(), Toast.LENGTH_SHORT).show();
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "Foto cancelada", Toast.LENGTH_SHORT).show();
}
break;
}
}
的事情是我已經在Android 4.0.1的三星Nexus S設備和Android 2.3.6的三星Galaxy Mini上測試過這個代碼,並且它的工作非常完美。但是,當我嘗試在三星Galaxy S3和三星Glaxy注2,既與Android測試代碼4.1.1行
storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/SmartRoutes", fileName);
回報總是空,所以storageDir = NULL,我不知道爲什麼。
它可以是關於Android版本4.1.1的東西嗎?
......你的問題出了什麼問題。 storageDir = new File(...),所以它不能爲NULL。 – ben75
對不起,我的啞巴問題,但你有一個外部SD卡在S3和注2是正確的? – NeilMonday