Reputation: 9975
I'm trying to save Bitmap that I have in certain activity to External Storage in a directory that I create for this purpose. The method for saving the image to sdcard (external memory, not external sd) is in a different class (and different package) so I assumed I will need the context, but I can't find any place to give the context (tried openFileOutput
but it gets String fileName
that can't contain path seperators). When I run my code I get this error in log cat:
04-11 22:13:14.899: E/error(13833): /mnt/sdcard/myTomatoes/covers/378194.PNG: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): java.io.FileNotFoundException: /mnt/sdcard/myTomatoes/covers/378194.PNG: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.IoBridge.open(IoBridge.java:416)
04-11 22:13:14.899: W/System.err(13833): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-11 22:13:14.899: W/System.err(13833): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-11 22:13:14.899: W/System.err(13833): at BL.ImageMethods.SaveImageToMemory(ImageMethods.java:60)
04-11 22:13:14.899: W/System.err(13833): at com.example.mytomatoes.MovieDetailsActivity$4.onClick(MovieDetailsActivity.java:337)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-11 22:13:14.899: W/System.err(13833): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 22:13:14.899: W/System.err(13833): at android.os.Looper.loop(Looper.java:137)
04-11 22:13:14.899: W/System.err(13833): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-11 22:13:14.899: W/System.err(13833): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 22:13:14.899: W/System.err(13833): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-11 22:13:14.899: W/System.err(13833): at dalvik.system.NativeStart.main(Native Method)
04-11 22:13:14.899: W/System.err(13833): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.Posix.open(Native Method)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.IoBridge.open(IoBridge.java:400)
Those are my permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This is my method for saving a file:
public static void SaveImageToMemory(Context context, Bitmap img, int rottenId) {
Log.i("SAVE IMAGE", "start save");
File sd = Environment.getExternalStorageDirectory();
File location = new File(sd.getAbsolutePath()+ "/myTomatoes/covers");
location.mkdir();
File dest = new File(location, rottenId + ".PNG");
try {
Log.i("SAVE IMAGE", "trying to save: " + dest.getPath());
FileOutputStream fos = new FileOutputStream(dest);
img.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
Log.e("error", e.getMessage());
e.printStackTrace();
}
}
All the messed up lines for creating the File
object are result of my try outs... The original code was:
File location = new File(Environment.getExternalStorageDirectory() + "/myTomatoes/covers");
File dest = new File(location, rottenId + ".PNG");
I also tried to first create ByteArrayOutputStream
and compress the Bitmap to it and then do fos.write(mByteArrayOpStream.toByteArray())
and it didn't work.
By the way i'm using emulator but I also checked this on few "Galaxy S"s and the same error happens... (when I try to browse for the file in the phone or in DDMS I can see that it doesn't even create the folder...
Upvotes: 4
Views: 6327
Reputation: 101
Hope this helps to store image in cache directory from android application and display that image in another activity.
Oneactivity.java
storing image in the cache directory
File file = new File(getCacheDir(), "thursday.png");
try {
OutputStream output;
output = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (Exception e) {
e.printStackTrace();
}
fetching image from cache directory.
BitMap bmp5;
try {
File myFile = new File(getCacheDir()+"/thursday.png");
bmp5 = BitmapFactory.decodeFile(myFile.toString());
System.out.println("am setting wallpaper");
myWallpaperManager.setBitmap(bmp5);
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 1856
just check this out :
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
{
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/Pictures");
dir.mkdirs();
File file = new File(dir, filename);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
FileOutputStream f = null;
f = new FileOutputStream(file);
if (f != null) {
f.write(baos.toByteArray());
f.flush();
f.close();
}
}
}
or follow this link :http://android-er.blogspot.in/2010/07/save-file-to-sd-card.html
Upvotes: 0
Reputation: 18978
try this code:
File newD = new File(Environment.getExternalStorageDirectory()
+ File.separator +"myTomatoes"+ File.separator + "covers");
if(!newD.exists()){
newD.mkdirs();
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File( newD + File.separator + "test.jpg")
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
Upvotes: 1
Reputation: 4233
From your problem description it seems like you couldn't even create the folder. So make sure the followings
Directory is created
if (!file.isExist()) boolean created=file.mkdir();
later you can check if your folder is created or not by testing the Boolean value. if your folder is not created no reason to go further with your code.
Alternatively you can use mkdirs()
which will create all the necessary parent directory.
Upvotes: 2