I am trying to save the following bitmap either in the external storage, if available and if not available, to save it in the internal storage. I thought that the following code should work but when I removed my sd card, I am still getting the toast message from the sd card section. I can get it to work if I just use either the external or internal section of the code but can't seem to figure out how to get both of them working together...
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = findViewById(R.id.form_linear_layout);
Bitmap bitmap = getBitmapFromView(ll);
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
pathInternal = saveInternalImage(bitmap);
//If it isn't mounted - we can't write into it.
// return "";
} else {
path = saveImage(bitmap);
}
}
public String saveInternalImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getDataDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
Log.d("hhhhh", wallpaperDirectory.toString());
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(MainActivity.this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
// Toast.makeText(MainActivity.this, "You have successfully saved into the phone internal memory!", Toast.LENGTH_SHORT).show();
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(MainActivity.this, "You have successfully saved into the phone internal memory!", Toast.LENGTH_SHORT).show();
return "";
}
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(MainActivity.this, "You do not have an SD Card!", Toast.LENGTH_SHORT).show();
//If it isn't mounted - we can't write into it.
// return "";
}
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
Log.d("hhhhh", wallpaperDirectory.toString());
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(MainActivity.this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
// Toast.makeText(MainActivity.this, "You have successfully saved into the phone External memory!", Toast.LENGTH_SHORT).show();
// return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(MainActivity.this, "You have successfully saved into the phone External memory!", Toast.LENGTH_SHORT).show();
return "";
}
}