android error trying to write to external SD - android

I am trying to write a jpg image to the external SD card. However, I am getting System.err FileNotFoundException: /mnt/sdcard/test.images/temp/savedImage (no such file or directory). Creating the directory also seems to fail and gives a false in LogCat and I also cannot see the folder when looking on my SD card.
My code is as follows:
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File folder = new File(Environment.getExternalStorageDirectory() + "/test.images/temp");
try {
if(!folder.exists()){
boolean dir = new File(Environment.getExternalStorageDirectory() + "/test.images/temp").mkdir();
Log.v("creating directory", Boolean.toString(dir));
}
File imageOutputFile = new File(Environment.getExternalStorageDirectory() + "/test.images/temp", "savedImage");
FileOutputStream fos = new FileOutputStream(imageOutputFile);
Image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I have permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the manifest and have cleaned and rebuilt.

Use mkdirs() instead of mkdir().
Guido posted a solution that works for me in the comment. I am going to repeat it just to make sure it can be an answer.

Related

Writing a file to external storage android FileNotFoundException

I want to write a file to external storage in android but get the FileNotFoundException. I have put the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
in my manifest and if I do a check like
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
it returns true. I have tried different solutions from questions from SO (Android saving file to external storage; Write a file in external storage in Android; Writing to external storage filenotfoundexception) but none seemed to work for me. Sorry for posting a duplicate question but Ive been searching for days and couldnt find an answer.
My Code is the following:
private void Writing(String erg) {
TextView one = (TextView) findViewById(R.id.e1);
File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File dir = new File (root.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, "Test.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println(erg);
pw.flush();
pw.close();
f.close();
one.setText("File created");
} catch (FileNotFoundException e) {
e.printStackTrace();
one.setText("Error1");
} catch (IOException e) {
e.printStackTrace();
one.setText("Error2");
}
}
The Textview one ends up with printing Error1.
I'll appreciate any help.

No such file or directory when trying to save file

For an App I am developing, I want to write some text to a text file on the SD card. I've tried to do that with the code below. This gives an error but I can't see why. As far as I can tell I have followed all the examples on the web perfectly. In logcat I see the first log, but not the second one so the problem is in the creation of the file. Do you guys have an idea what's going wrong?
public void saveDataToFile(String data, String fileName) {
Log.d("Checks", "Trying to save data");
try {
// Set up the file directory
String filePath = Environment.getExternalStorageDirectory().toString() + "/Data Folder";
File fileDirectory = new File(filePath);
fileDirectory.mkdirs();
Log.d("Checks", "Directory created");
// Set up the file itself
File textFile = new File(fileDirectory, fileName);
textFile.createNewFile();
Log.d("Checks", "File created");
// Write to the file
FileOutputStream fileOutputStream = new FileOutputStream(textFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.append(data);
outputStreamWriter.close();
fileOutputStream.close();
Toast.makeText(mContext, "Done writing to SD card", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
EDIT:
Turns out I had forgotten to add the right permission to the manifest. It works now!
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Did you declare the proper uses permissions to access and write to the External SD card ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android does not make director(mkdir)

SOLVED
i change it a litle bit
File path = new File(Environment.getExternalStorageDirectory(), "AndroidPaint");
File file = new File(path, tmpImg);
path.mkdirs();
if(!file.exists()) {
file.createNewFile();
}
os = new FileOutputStream(file);
source.compress(CompressFormat.JPEG, 100, os);
os.flush();
os.close();
also problem could be source Bitmap i discovered then was null
PROBLEM
on my Android program i want save bitmap on external store i got permission for do that
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and got some code for that
public class SaveOpen {
public static void save(Bitmap source) {
String tmpImg = String.valueOf(System.currentTimeMillis()) + ".jpg";
OutputStream os = null;
try {
File dir = new File(Environment.getExternalStorageDirectory(), "myapp");
if (!dir.mkdirs()) {
Log.e("save", "Directory not created");
}
File file = new File(dir, tmpImg);
if(!file.exists()) {
file.createNewFile();
}
os = new FileOutputStream(file);
source.compress(CompressFormat.JPEG, 100, os);
os.flush();
os.close();
}
catch (IOException e) {
Log.d("save", e.getMessage());
}
}
}
but when i press save button on my app i get
open failed: ENOTDIR (Not a directory)
exception, what i do wrong.? i tried all guide i found in google, pls help
try this:
String myDir = Environment.getExternalStorageDirectory() + "/myapp";
File dir = new File(myDir);
On older devices access to external storge is not possible if there's is USB connection (disk mode), so log what is Environment.getExternalStorageDirectory() returning in your case.
I met the same problem, it seems like a bug on Android, I mkdir in the command line by adb, also failed. Then I try to restart the phone, never happen again.
My problem solved by add permissions to access external storage:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Saved Bitmap doesn't appear on SD Card

I am working on an app in which I would like to save some Bitmaps to the SD Card. I have looked at a lot of examples and other questions, and from that I have made the following code:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String dirPath = Environment.getExternalStorageDirectory().toString() + "/myFolder";
File dir = new File(dirPath);
dir.mkdirs();
String fileName = "bitmapname.jpg";
File file = new File(dirPath, fileName);
FileOutputStream fileOutPutStream;
try {
boolean created = file.createNewFile();
Log.d("Checks", "File created: " + created);
fileOutPutStream = new FileOutputStream(file);
fileOutPutStream.write(byteArrayOutputStream.toByteArray());
fileOutPutStream.close();
} catch (FileNotFoundException e) {
Log.d("Checks", "FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
Log.d("Checks", "IOException");
Log.d("Checks", e.getMessage());
e.printStackTrace();
}
I don't see what's wrong with this code. It doesn't give any errors and my app runs without crashing. However, when I connect my phone to my computer and open the SD Card I do not see the folder "myFolder" and I can not find the saved image anywhere. Do you guys have any ideas as to why this is?
EDIT: I noticed that I can see the saved bitmaps in the Android gallery, and they are indeed in a folder called "myFolder". However, I still don't see them when I connect my phone to my computer and browse my sd card.
From my experience I had similar issued when I forgot the fileOutPutStream.flush(); before the close().
Are you sure you are setting the permission to write to SD card? Try setting this one:
WRITE_EXTERNAL_STORAGE
Edit:
Ok, try this:
Environment.getExternalStorageDirectory().getAbsolutePath()
Instead of:
Environment.getExternalStorageDirectory().toString()
Or even create a directory like this:
File dir = new File(Environment.getExternalStorageDirectory() +
File.separator +
"myFolder");
dir.mkdirs();

Storing data on SD Card in Android

Using the data-storage page in the docs, I've tried to store some data to the SD-Card.
This is my code:
// Path to write files to
String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/Android/data/"+ctxt.getString(R.string.package_name)+"/files/";
String fname = "mytest.txt";
// Current state of the external media
String extState = Environment.getExternalStorageState();
// External media can be written onto
if (extState.equals(Environment.MEDIA_MOUNTED))
{
try {
// Make sure the path exists
boolean exists = (new File(path)).exists();
if (!exists){ new File(path).mkdirs(); }
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + fname);
fOut.write("Test".getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
When I create the new FileOutputStream I get a FileNotFound exception. I have also noticed that "mkdirs()" does not seem to create the directory.
Can anyone tell me what I'm doing wrong?
I'm testing on an AVD with a 2GB sd card and "hw.sdCard: yes", the File Explorer of DDMS in Eclipse tells me that the only directory on the sdcard is "LOST.DIR".
Have you given your application permission to write to the SD Card?
You do this by adding the following to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Before reading or writing to SD card, don't forget to check the SD card is mounted or not?
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

Categories

Resources