I'm building an app to take picture from camera but I don't know that I can save my photo of my app on sd card in avd android (my AVD: Nexus 5X API 26 Android 8.0). I took photo and showed my photo on my app but I can't find it in my folder I created for my app (in SDCard). Thank you.
Asked
Active
Viewed 458 times
0
-
1Possible duplicate of [Android saving file to external storage](https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage) – Umer Kiani Dec 24 '18 at 09:57
1 Answers
1
first add the permission in your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and so you need to get your Bitmap.you can try to get it from the ImageView such as:
BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();
you must get to directory from SD Card
File sdCardDirectory = Environment.getExternalStorageDirectory();
// Next, create your specific file for image storage:
File image = new File(sdCardDirectory, "test.png");
//After that, you just have to write the Bitmap :
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
-
-
@eyllanesc hi , I want to add permission but when send isn't show ! thanks ! – hamid Dec 24 '18 at 10:04
-