The code seems to work fine. The problem is that afterwards in my gallery I do not have an folder "myFolder" with my pics in it. Instead the pics get saved to storage/sdcard0/DCIM/camera and myFolder doesnt show up.
myDirectory = final static String myDirectory = "myFolder".
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
final File imageRoot = new File(Environment.getExternalStorageDirectory(), myDirectory);
if (!imageRoot.exists()) {
imageRoot.mkdirs();
}
File file = new File(imageRoot, fileName);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Related
I want to save the Uri or pathc from a image saved in the internal storage for after get and put into viewImage form Picasso, but this does not work.
public File SaveImage(Bitmap ImageToSave) {
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + NameOfFolder;
String CurrentDateAndTime = getCurrentDateAndTime();
File dir = new File(file_path);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, NameOfFile + CurrentDateAndTime + ".jpg");
try {
FileOutputStream fOut = new FileOutputStream(file);
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MakeSureFileWasCreatedThenMakeAvabile(file);
AbleToSave();
}
catch(FileNotFoundException e) {
UnableToSave();
}
catch(IOException e) {
UnableToSave();
}
//return file.getPath();
//return file.getAbsolutePath();
return file;
}
I save the image.
File file = saveImage(currentBitmap);
Picasso.get().load(file.getAbsolutePath()).into(imageView);
But never load the image
Note: currentBitmap Can not be used(because it is for save in Realm database and this take up to much space).
Thanks for your support.
How to write image in phone directory. Directory is created but i could not write image in this folder. Here is my code .please check if i am doing something wrong thanks in advance.
Bitmap bitmap;
String directory = new_path; // I am getting path here of image like sdcard/0/emulated/image.jpg
String folder_name = "abc"
bitmap = BitmapFactory.decodeFile(directory);
iv_6.setImageBitmap(bitmap); // image displayed here but not saving in directory.
try {
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator +folder_name);
f.mkdirs();
FileOutputStream fo = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fo);
fo.close();
}catch (Exception e)
{
e.printStackTrace();
}
public void savePng(Bitmap bitmap, String filePath) {
try {
File temp = new File(filePath);
FileOutputStream os = new FileOutputStream(temp + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
This question already has answers here:
Android - Save images in an specific folder
(5 answers)
Closed 6 years ago.
i want to capture an image and save it into specific folder rather than in DCIM/Camera or Gallery...
want to save like: storage/sdcard0/DCIM/MyFolder.
Try this one it may be help you
public void takePicture(){
Intent imgIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "ImagesApp");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "temp.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imgIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imgIntent,IMAGE_CAPTURE_REQUEST_CODE);
}
You can use the following code
private String save(Bitmap bitmap)
{
File save_path = null;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
try
{
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/DirName");
dir.mkdirs();
File file = new File(dir, "DirName_"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime())+ ".png");
save_path = file;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,baos);
FileOutputStream f = null;
f = new FileOutputStream(file);
MediaScannerConnection.scanFile(this, new String[]{file.getAbsolutePath()}, null, null);
if (f != null)
{
f.write(baos.toByteArray());
f.flush();
f.close();
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
return String.valueOf(save_path);
}
Hope this will help you out...
Try following code
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/DirName/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/DirName/"), fileName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I've googled a lot of information about my issue but I really don't get what i'm doing wrong, I just want to save an image to sdcard in specific folder, but folder.mkdir() always return false and I get an exception
java.io.FileNotFoundException: /mnt/sdcard/sakhcomcache/tv/1.gif: open failed: ENOENT (No such file or directory)
and of course I have a permission in my manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here is my code
.....
private final static String CACHE_PATH = "/sakhcomcache/";
public final static String CACHE_FOLDER_TV = "/tv";
.....
public static void saveImageOnSDCard(final Bitmap image, final String cacheFolder, final String name) {
new Thread(new Runnable() {
#Override
public void run() {
try {
File folder = new File(Environment.getExternalStorageDirectory() + CACHE_PATH + cacheFolder);
if (!folder.exists()) {
boolean create_succes = folder.mkdir();
//create_succes always false
if(create_succes){
Log.i("create_succes", "create_succes");
}
}
File imageFile = new File(folder +"/"+ name.substring(name.lastIndexOf("/")));
if (!imageFile.exists()) {
FileOutputStream out = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Log.i("save succes", "save succes");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
try this
//creating directory
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
directoryCreated = new File(root, "filename");
//saving file
FileOutputStream out = new FileOutputStream(directoryCreated);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();
I have more then 50 images in my res/drawable folder. I want to save these images to external storage and then display these image one by one in an image view/image switcher. I used below code to save single image to external storage. but i am unable to figure it out how can i save all these images to external storage altogether (at once).
public void SaveImage(){
if (!CheckExternalStorage()) {
return;
}
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a01);
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(path, "image1.png");
file.createNewFile();
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
Log.i(LOGTAG, "Image Written to Exterbal Storage");
} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}
}
Using an answer from : https://stackoverflow.com/a/3221787/794088 with some modification to call your method SaveImage with a param
...
R.drawable drawableResources = new R.drawable();
Class<R.drawable> c = R.drawable.class;
Field[] fields = c.getDeclaredFields();
for (int i = 0, max = fields.length; i < max; i++) {
final int resourceId;
try {
resourceId = fields[i].getInt(drawableResources);
// call save with param of resourceId
SaveImage(resourceId);
} catch (Exception e) {
continue;
}
}
...
public void SaveImage(int resId){
if (!CheckExternalStorage()) {
return;
}
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resID);
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(path, "image1.png");
file.createNewFile();
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
Log.i(LOGTAG, "Image Written to Exterbal Storage");
} catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}
}