My problem is whenever I save the bitmap. I saves one in the folder I want(MyFolder) and one in the DCIM/Camera. I don't see any code that makes it save int he DCIM directory?
Here are my codes...
case R.id.menu_save:
try {
String path = Environment.getExternalStorageDirectory()
.toString();
File myNewFolder = new File(path + "/MyFolder");
myNewFolder.mkdirs();
OutputStream fOut = null;
File file = new File(path, "/MyFolder/HK" + filename + ".jpg");
fOut = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
Toast.makeText(getApplicationContext(),
filename + "Has been saved!", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(
getApplicationContext(),
"Problem to Save the File", Toast.LENGTH_LONG).show();
}
break;
As in the thread #Dixit linked, you can specify the file path URI with
File fileDir = new File(Environment.getExternalStorageDirectory() +
"/saved_images");
fileDir.mkdirs();
File file = new File(fileDir, "image.jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
This way the camera will save the image to the specified path instead of the DCIM folder.
EDIT: You have to create the folder on the sdcard beforehand, maybe that's the problem. Otherwise, this should work.
Related
I'm looking to be able to take png images (called 1.png, 2.png, 3.png) that I have stored in my apps drawable folder and copy/save them to a file directory that is created by my app on launch.I've tried .createnewfile() but was unable to get anything to work.code for creating my file directory below:
//-----create file directory for images
public void createDirectory() {
try {
File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES");
if (!imageDirectory.exists()) {
imageDirectory.mkdirs();
//something here to take images from drawable folder and place them into my newely created directory
File imageFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES", //something here getDrawable maybe?
);
imageFile.createNewFile();
}
}
catch (Exception e) {
Log.d("MAKE_DIR", "error creating directory");
}
}
thanks
Solved, this is how I done it:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maptile_1);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES"
+ File.separator + "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
toastMsg = e.toString();
toast();
}
I'm working on app which will read folders and files from an SD card, now to need to create a raw image file which will emulate the SD card, but when I use mksdcard it only creates an empty raew image file.
void saveImage() {
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new File(path + "/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName());
Toast.makeText(getApplicationContext(), "File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}
I save an image to the sdcard and it doesn't appear in the Phone's Gallery. I can see saved image in a folder, but it folder doesn't apper in gallery.
My codes here, how fix it?
img_icon.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
img_resim.buildDrawingCache();
Bitmap bm = img_resim.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri;
try {
root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(context.getActivity(),
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
context.getActivity()
.sendBroadcast(
new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"
+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
return true;
}
});
return rowView;
}
Thanks for helping..
Your problem lies here:
context.getActivity()
.sendBroadcast(
new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"
+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
You are asking Android to re-index all of the files in Environment.DIRECTORY_PICTURES.
Asking to scan a whole directory tree is wasteful. In your case, it it even more wasteful, since you are not writing your file to that directory. Instead, you are writing that file to:
root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
Hence, your scan will not pick up this file, which you are writing to some random spot on external storage.
You need to decide the proper place to store your file, then index that one file.
I'm a newbie Android developer. I have loaded an image using universal-image-loader and I would like to save it on my sd card. The file is created in the desired directory with the correct filename, but it always has a size of 0. What am I doing wrong?
A relevant snippet follows:
PS: The image already exists on disk, it's not being downloaded from the Internet.
private void saveImage(String imageUrls2, String de) {
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
String filename = de;
File myDir = new File(SDCardRoot+"/testdir");
Bitmap mSaveBit = imageLoader.getMemoryCache();
File imageFile = null;
try {
//create our directory if it does'nt exist
if (!myDir.exists())
myDir.mkdirs();
File file = new File(myDir, filename);
if (file.exists())
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bos.flush();
bos.close();
} catch (IOException e) {
filepath = null;
e.printStackTrace();
Toast.makeText(getApplicationContext(),
R.string.diskful_error_message, Toast.LENGTH_LONG)
.show();
}
Log.i("filepath:", " " + filepath);
}
Yes, your code creates an file on sdcard_root/testdir/de only, and didn't write anything to it. Is "imageUrls2" the source image file? If yes, you can open that file with BufferedInputStream, read the data from BufferedInputStream, and copy them to output file with bos.write() before bos.flush() and bos.close().
Hope it helps.
I'm trying to write an image file into the public gallery folder in a specific directory but I keep getting an error that I can't open the file because its a directory.
What I have so far is the following
//set the file path
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
File outputFile = new File(path,"testing.png");
outputFile.mkdirs();
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Where directory is the application name. So all the photos saved by the application will go into that folder/directory, but I keep getting the error
/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)
Even if I don't try to put it in a directory and cast the variable path as a File like
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
I don't get the error however the photo is still not showing up in the gallery.
***Answer
The problem was that when I ran this code originally it created a DIRECTORY named testing.png because I failed to create the directory before creating the file IN the directory. So the solution is to make the directory first then write into it with a separate file like so
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;
//directory is a static string variable defined in the class
//make a file with the directory
File outputDir = new File(path);
//create dir if not there
if (!outputDir.exists()) {
outputDir.mkdir();
}
//make another file with the full path AND the image this time, resized is a static string
File outputFile = new File(path+File.separator+resized);
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Note you may need to go into your storage and manually delete the directory if you made the same mistake i did to begin with
You are trying to write into a directory instead of file.
try this
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
File outputDir= new File(path);
outputDir.mkdirs();
File newFile = new File(path + File.separator + "test.png");
FileOutputStream out = new FileOutputStream(newFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Your code is correct, only little changes needs as follows,
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
// First Create Directory
File outputFile = new File(path);
outputFile.mkdirs();
// Now Create File
outputFile = new File(path,"testing.png");
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Also don't forget to give WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file.
If you are getting this error while working on Android Emulator; you need to enable SD Card storage on the emulator.
public static String SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/FolderName");
if(!myDir.exists())
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
getExternalStoragePublicDirectory is now deprecated and you should use
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
Use this way :
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/mnt/sdcard/" + new Date().getTime() + ".jpg"));`
Path : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + file name