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.
Related
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()
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
I'm having a problem. I'm trying to create a bitmap in a directory on my phone. However it doesn't always work and never seems to work right away. After running if I look in the directory it wont be there, I have to fiddle around in other folders and keep refreshing before it shows up, often it doesn't even show up at all. I do not understand this, any help or insight would be very much appreciated, thanks
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myImage.compress(Bitmap.CompressFormat.PNG, 40, bytes);
try {
File f = new File(Environment.getExternalStorageDirectory() + "/myDirectory/" + "test.png");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Log.e(TAG, "Was unable to copy image" + e.toString());
}
If for some reason someone comes across this with a similar problem, I found out the solution. For whatever reason the file wont show up until I disconnect the phone from the USB cable. The second I do the files show up.
You are trying to create the file without having created the /myDirectory/ folder first, so it may fail to create your file. Also, you must check the return value of f.createNewFile(), because it will return false if the file already exists. For really checking if the file exists, use f.exists() instead.
So, if you change your code to:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Log.i("COMPRESSED Bitmap", " "+myImage.compress(Bitmap.CompressFormat.PNG, 40, bytes));
try {
File directory = new File(Environment.getExternalStorageDirectory() + "/myDirectory/");
Log.i("Created external folder successfully", " "+directory.mkdirs());
File f = new File(Environment.getExternalStorageDirectory() + "/myDirectory/" + "test.png");
Log.i("Created external file successfully", " "+f.createNewFile() + " file exists "+ f.exists());
if(f.exists()){
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
} catch (IOException e) {
e.printStackTrace();
Log.e("MyActivity", "Was unable to copy image" + e.toString());
}
It will only write to the file if it already exists. I tested it and the file is always there, and is replaced by another if you execute the code a second or third time.
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"/>
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.