Android writing bitmap to sdcard - java

I'm trying to write a bitmap to an sdcard on android, and I get the error message of
/mnt/sdcard/PhysicsSketchpad/sketchpad145.png (No such file or directory).
I declared the android.permission.WRITE_EXTERNAL_STORAGE permission in the manifest, and this is my code:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
What's going on?
UPDATE
It seems as though when I try to write to an existing directory, I get an permission denied error,
08-11 09:55:23.796: WARN/Physics Sketchpad(8881): Error when saving: IOException /mnt/sdcard/download/sketchpad54.png (Permission denied)
and when I try to save in a new directory I get a no such file or directory error, 08-11 09:59:20.175: WARN/Physics Sketchpad(9040): Error when saving: IOException /mnt/sdcard/PhysicsSketchpad/sketchpad55.png (No such file or directory)
In addition, File.mkdirs() returns a boolean based on if it succeeded or not, and it returned false.

try this code.
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad";
File dir = new File(file_path);
if(!dir.exists)
dir.mkdirs();
File file = new File(dir, "sketchpad" + pad.t_id + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();

try this code. This has worked for me.
public void saveBitmap(Bitmap bm)
{
try
{
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/";
String mFilePath = mBaseFolderPath + "abcd.jpg";
FileOutputStream stream = new FileOutputStream(mFilePath);
bm.compress(CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
}
catch(Exception e)
{
Log.e("Could not save", e.toString());
}
}
Shash

you getting the absolute path check into the error what you getting the path
/mnt/sdcard/AppName/appname145.png
and you set the
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/PhysicsSketchpad/";
where as "PhysicsSketchpad" dir not getting in above path
try this
Environment.getExternalStorageDirectory().toString()+ "/PhysicsSketchpad/";

Related

Sub Directories under getCacheDir()

I'm trying to create sub directories in my apps cache folder but when trying to retrieve the files I'm getting nothing. I have some code below on how I created the sub directory and how I'm reading from it, maybe I'm just doing something wrong (well clearly I am lol) or maybe this isn't possible? (though I haven't seen anywhere that you can't). thank you all for any help!
creating the sub dir
File file = new File(getApplicationContext().getCacheDir(), "SubDir");
File file2 = new File(file, each_filename);
Toast.makeText(getApplicationContext(), file2.toString(), Toast.LENGTH_SHORT).show();
stream = new FileOutputStream(file2);
stream.write(bytes);
reading from it
File file = new File(context.getCacheDir(), "SubDir");
File newFile = new File(file, filename);
Note note;
if (newFile.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try {
fis = new FileInputStream(new File(file, filename));
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
I've also tried with this and nothing
String file = context.getCacheDir() + File.separator + "SubDir";
I don't see anywhere in the code you posted where you actually create the sub-directory. Here's some example code to save a file in a sub-directory, by calling mkdirs if the path doesn't yet exist (some parts here need to be wrapped in an appropriate try-catch for an IOException, but this should get you started).
File cachePath = new File(context.getCacheDir(), "SubDir");
String filename = "test.jpeg";
boolean errs = false;
if( !cachePath.exists() ) {
// mkdir would work here too if your path is 1-deep or
// you know all the parent directories will always exist
errs = !cachePath.mkdirs();
}
if(!errs) {
FileOutputStream fout = new FileOutputStream(cachePath + "/" + filename);
fout.write(bytes.toByteArray());
fout.flush();
fout.close();
}
You need to make your directory with mkdir.
In your code:
File file = new File(getApplicationContext().getCacheDir(), "SubDir");
file.mkdir();
File file2 = new File(file, each_filename);

storage/emulated/0/ open faild:ENOENT (no such file or directory)?

i have below code that work in some phone and dont work in some other phone.
my code is for saving an image.
String path = Environment.getExternalStorageDirectory().toString();
path += "/";
File fff=new File(path + "/xalopex/Mobile/");
if (!fff.exists())
fff.mkdirs();
path += "xalopex/Mobile/";
path += "lg";
File filename;
try {
filename = new File(path + ".jpg");
FileOutputStream out = new FileOutputStream(filename);
MyImageBitMap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
} catch (Exception e) {
e.printStackTrace();
//this part will run
// e is storage/emulated/0/xalopex/Mobile/lg.jpg open faild:ENOENT (no such file or directory)
}
where i am wrong ?
How i can fix it?
I would hazard a guess and say its because you need to be accessing /storage/emulated/legacy instead of /storage/emulated/0
I could be wrong.. but I see there are no accepted answers so I'll give this a try :)

I keep getting java.io.FileNotFoundException with my zip extractor

Could anybody help me with my java zip extractor as stated in the title I keep getting java.io.FileNotFoundException on the folders with files in them
public void UnZip() {
try {
byte[] data = new byte[1000];
int byteRead;
BufferedOutputStream bout = null;
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(sourceFile)));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
String filename = entry.getName();
File newfile = new File(Deobf2 + File.separator + filename);
System.out.println("file unzip : " + newfile.getAbsoluteFile());
new File(newfile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newfile);
int len;
while ((len = zin.read(data)) > 0) {
fos.write(data, 0, len);
}
fos.close();
entry = zin.getNextEntry();
}
zin.closeEntry();
zin.close();
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}
error log
http://pastebin.com/crMKaa37
values
static String tempDir = System.getProperty("java.io.tmpdir");
public static File Deobf = new File(tempDir + "Deobf");
public static String Deobf2 = Deobf.toString();
entire code paste
http://pastebin.com/1vTfABR1
I have copy pasted same code and it is working fine. I think u dont have administrator permission on C drive. login As Administrator and run . it will work.
Access Denied Exception will come when u don have administrator level of permission on C drive.
The problem is Your doing
String Deobf2 = Deobf.toString();//this does not give the location of the file
use
file.getAbsolutePath();
in your case Deobf.getAbsolutePath();
instead. Check http://www.mkyong.com/java/how-to-get-the-filepath-of-a-file-in-java/
if you want to get the path only till the parent directory check this How to get absolute path of directory of a file?
Problem fixed changed some code
for anyone whos wants a copy of the working zip extraction code here you go http://pastebin.com/bXL8pUSg
The variable Deobf2 in the output of the zip

Write a file in SDcard in Android

I have been searching this problem.
It actually worked in older version of android, but after I updated SDK, I get an error.
Error message is "open filed: ENOTDIR (Not a directory): /sdcard/PlayNumbers/mysdfile.xml"
Can please someone point me what I did wrong??
My codes are below.
Many Thanks,
path=new File("/sdcard/PlayNumbers");
myFile = new File(path,"mysdfile.xml");
if (!path.exists()) {
path.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("test");
myOutWriter.close();
fOut.close();
==>
File path = null;
File myFile = null;
String filePath = Environment.getExternalStorageDirectory().toString();
path=new File(filePath+"/PlayNumbers/");
myFile = new File(path,"mysdfile.xml");
//i also tried both as below
//path=new File(filePath+"/PlayNumbers");
//myFile = new File(path,"mysdfile.xml");
if (!path.exists()) {
path.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("test");
myOutWriter.close();
fOut.close();
p.s. ok, I have changed as you guys mentioned like my code, but it still gives me same error that it is not directory... any idea???
This should work, assuming that you have the correct permission in your manifest:
File externalStorageDir = Environment.getExternalStorageDirectory();
File playNumbersDir = new File(externalStorageDir, "PlayNumbers");
File myFile = new File(playNumbersDir, "mysdfile.xml");
if (!playNumbersDir.exists()) {
playNumbersDir.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
you just need to change to the following code because you missing "/":
myFile = new File(path,"/mysdfile.xml");
But keep in mind that you have to have the permission for writing in external storage in your manifest file :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

how to open a file on android

I am new to android and programming. I am using the android touchpaint api, and using the following code to save a drawing, but obviously saving is useless without being able to open the file.
I was wondering if anyone could help me with some code for it.
// Function saves image to file
public String save(Bitmap mBitmap, boolean showMsg) {
String filename;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
filename = sdf.format(date);
try {
String path = Environment.getExternalStorageDirectory().toString() + "/modTouchPaint/";
File file1 = new File(path);
file1.mkdirs();
OutputStream fOut = null;
File file = new File(path, filename + ".jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
if (showMsg)
Toast.makeText(this, "Picture saved to " + path + filename + ".jpg", 9000).show();
return path + filename + ".jpg";
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Please make sure that SD card is installed", 5000).show();
return null;
}
}
Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
See the documentation for more information.

Categories