Android does not make director(mkdir) - android

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"/>

Related

No file directory : java.io.IOException: No such file or directory

I'm working on an Android project and i wanna get the path of the uploaded picture from Camera or Gallery. All the permissions are set and I use this function to get the path but it seems createNewFile() is always ignored and i get path="" all the time.
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
but i get a problem on the log
There could be something the way you put together your filepath, but this is not viewable by your post. You could have missed giving permissions in the manifest, so the file is not created.
Try this:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
Make sure you have an External SD card in your phone as you are trying to access
Environment.getExternalStorageDirectory()

java.io.fileNotFoundException(Permission denied)

I have the next method :
public void saveOnSDCard() {
File path = new File("storage/sdcard1");
File dir = new File(path + "");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "test.png");
OutputStream os;
try {
os = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (IOException ioe) {
showToast("Ошибка " + ioe.toString());
}
}
but when i try to call it, it gives me a java.io.fileNotFoundException(Permission denied).
I have the next permission in my Manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
So can someone tell me what's wrong ?
P.S image - Bitmap object, i declared it in another method.
try if one of the following solutions works:
replace "storage/sdcard1" with"storage/sdcard1/"
replace path + "" with path + "/"
replace "test.png" with "/test.png"
I think it's just missing a slash in one of those three lines
Guy in comment told me that if you have an android 4.4+ you can write to sdcard only when you make your sdcard as primary memory or sometimes when ur phone is rooted. I tried it and it really works.

Android save text file

The question is pretty common and I have googled it a few but I still have problems. I have to save in a text file some data for a game that I am trying to develop. This is the code:
if (!isSecond(game[k])) {
//Where I'd like to save the file
File myFile = new File(Environment.getExternalStorageDirectory() + "/flagQuiz/record.txt");
//Try to save my file
try {
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
//correctGuess and TimeElapsed are two integers
myOutWriter.append(String.valueOf(correctGuess) + "-" + String.valueOf(TimeElapsed));
myOutWriter.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The log cat shows me this kind of error:
I have declared the possibility to save stuff in the SD Card in this way I guess:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Where is the mistake? flagQuiz folder is already created at runtime.
Be sure to check if file.exists() if true then delete it and create new one
else false then just carry on with your try block

android error trying to write to external SD

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.

Can't write file on SD-card (Is a directory)

I'm getting the following error when trying to write midi-file on the Android SD-Card:
12-14 16:22:22.219: ERROR/Thread writer(1108): java.io.FileNotFoundException: /mnt/sdcard/folder/midifiles/file.mid (Is a directory) in /mnt/sdcard/folder/midifiles/
That's the relevant code:
public void writeFile(String filename, String dir, int bpm) throws Exception {
File f = new File(dir,filename);
if(!f.exists()) {
f.mkdirs();
if(!f.createNewFile()) {
return;
}
}
FileOutputStream fos = new FileOutputStream(f);
I'm receiving the path via
Environment.getExternalStorageDirectory()
The relevant permission has been included as well.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The file-writer is located in an external project/library... if this is in any case useful to know.
I guess that this is kind of a simple mistake, but I honestly have no idea what went wrong here.
Thanks in advance.
You are creating the directory /mnt/sdcard/folder/midifiles/file.mid with
f.mkdirs();
You should just do:
File f = new File(dir);
if(!f.exists()) {
f.mkdirs();
File f1 = new File(dir,filename);
if(!f1.createNewFile()) {
return;
}
}
or just
File f = new File(dir,filename);
if(!f.exist()) {
f.createNewFile();
return;
}
Are you running this on your phone? or connected to your pc? because it may be looking on your pc.

Categories

Resources