Ich versuche folgendes:Speichern Foto SD erstellen keine Datei
1. Click a button to take a photo.
2. Save photo.
3. Add photo into the gallery.
4. Show photo into an ImageView.
1
und 4
funktioniert gut, aber ich habe Probleme mit 2
und 3
.
Dies ist mein Code:
photoFile = createImageFile();
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp;
File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg");
mCurrentPhotoPath = imageFile.getAbsolutePath();
return imageFile;
}
Damit habe ich ein filePath geschaffen, wo ich das Bild gespeichert werden soll.
Nun rufe ich die Intent
mit extra params das Bild zu speichern:
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getContext(),
"es.antonio.prueba.fileprovider",
photoFile);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
}
nun in meiner onActivityResult
ich eine Funktion aufrufen, das Foto in der Galerie hinzuzufügen:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getContext().sendBroadcast(mediaScanIntent);
}
Und ein anderes um das Foto in die ImageView
zu setzen:
private void setPic(ImageView mImageView) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
}
Ich habe Probleme damit, das in SD aufgenommene Bild zu speichern, da meine Methode nicht funktioniert (ich bin Android Entwickler tutrial). Ich dachte, dass das Übergeben eines Extra an die Absicht den Trick machen sollte, aber es funktioniert nicht.
Irgendwelche Hinweise?
Berechtigungen im Manifest geschrieben? – Ivan
Ja, für Kamera hinzugefügt und externen Speicher lesen/schreiben – canillas
'createNewFile();' gibt wahr oder falsch zurück. Sie überprüfen den Rückgabewert nicht. Bitte fügen Sie Code hinzu, um dies zu tun und Toasts, um den Benutzer zu informieren. – greenapps