Nella mia app l'immagine è salvata sulla scheda SD, quindi l'utente deve scegliere cosa fare con esso. Elimina, salva o invia. Se si preme cancella. Io chiamo File.delete()
cancella i file quando vado a guardare nella galleria e poi quando a volte torno più tardi vedo un'immagine nera che dice che il file non può caricare. Quella immagine è quella che ho tentato di cancellare prima. Cosa c'è di sbagliato in questo e perché non scompare del tutto?File.delete() non cancella completamente il file immagine vuoto dell'immagine lasciato in ritardo
Come posso salvare l'immagine:
public static File getOutputMediaFile(byte[] data){
image= BitmapFactory.decodeByteArray(data, 0, data.length);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FrontFlash");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()) return null;
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
@Override
public void onPictureTaken(byte[] data, Camera camera){
pictureFile = Util.getOutputMediaFile(data);
if (pictureFile == null){
Toast.makeText(this, "Couldn't create file", Toast.LENGTH_SHORT).show();
}
else{
try{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}
catch (FileNotFoundException e){
Toast.makeText(this, "File not found exception", Toast.LENGTH_SHORT).show();
}
catch (IOException e){
Toast.makeText(this, "IO Exception", Toast.LENGTH_SHORT).show();
}
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FrontCam"))));
String photopath = pictureFile.getPath().toString();
// Ruota l'immagine
bmp = BitmapFactory.decodeFile(photopath);
matrix = new Matrix();
matrix.postRotate(270);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(pictureFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dove onClick dove l'immagine è cancellare
public void exit(View view){
deleted = pictureFile.delete();
//startActivity(home);
close();
finish();
}
Questo perché la galleria non è stato aggiornato. Attivare una scansione multimediale e dovrebbe andare bene. –
non è quello che fa quando salva la foto? 'sendBroadcast (new Intent (Intent.ACTION_MEDIA_MOUNTED, Uri.parse (" file: // "+ new File (Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES)," FrontCam "))));' –
in caso contrario, per favore mostrami cosa mettere dopo cancellare l'immagine –