I have gotten this message when I try to save an imagen file ...
java.io.FileNotFoundException: /logocte1 (Read-only file system)
My method ...
public static String saveFile(Bitmap bitmap, String filename) {
String stored = null;
File file = new File(filename) ;
if (file.exists())
return stored ;
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
stored = "success";
} catch (Exception e) {
e.printStackTrace();
}
return stored;
}
I want to write the file INTO internal menory ... I don't have external memory in my pone.
I have gotten this message when I try to save an imagen file
You cannot write to arbitrary locations. Please use methods like getFilesDir() and getExternalFilesDir() for directories that you can write to.
I don't have external memory in my pone.
Most likely, you do. External storage is not the same as removable storage, and neither of those are the same as internal storage.
Related
I know there are many post on Environment.getExternalStorageDirectory() for finding out folder for saving files. When I used it, I am getting
/storage/emulated/0/myfolder
as folder path and getting below error when try to use for saving file.
w/System.err: java.io.FileNotFoundException: /storage/emulated/0/myfolder/myfile (No such file or directory)
How to resolve this issue? I would like to use Internal Memory for storing Image & video in the same fashion as other app does.
Thanks in advance for your help.
Update : As requested Code for saving Image that is been created.
public String saveTofolder(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File folder = new File(sdcard.getAbsoluteFile(), "myfolder");
if (!folder.exists()) folder.mkdirs();
File file = new File(folder.getAbsoluteFile(), filename);
if (!file.exists()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
stored = "success";
}
return stored;
}
I'm trying to save bitmap to gallery to open it again from ImagePicker intent.
The problem is - if I'm saving it on external storage, it will not work on devices without it, and when I'm writing it on internal storage - it won't shown in gallery, so i can not pick that image.
Tried so many ways of resolving it, but no success this far.
I mean, is it possible to save bitmap in internal storage and open it from activity, which called to choose image from gallery?
edit: added code
private String saveToInternalStorage(Bitmap bitmapImage, String filename){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,filename + ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
With that procedure it's seems like saving bitmap is ok, but i can't find that file in gallery.
I wasn't sure how to word this, but in iOS terms, if I download an image, and save it in the documents directory, it saves it in the Apps Container, which is not visible to other apps, and camera roll etc.
The way I have it in Android currently, all of these images are visible in the File Explorer and Gallery.
I was wondering how I could save these images in a similar way to iOS and have them hidden in the Apps own container.
Is there a way to create a folder, with context.MODE_PRIVATE or something similiar?
This is what I have currently, does it do the trick?
public static Boolean saveToInternalStorage(Context context, Bitmap bitmap, String filename) {
ContextWrapper cw = new ContextWrapper(context);
//Path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
//Create imageDir
File mypath = new File(directory, filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
//Use the compress method on the BitMap object to write image to the OutputStream
if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public static Bitmap loadImageFromStorage(Context context, String filename) {
ContextWrapper cw = new ContextWrapper(context);
//Path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
try {
File f = new File(directory, filename);
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
Read this article. This will help..
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
I was wondering how I could save these images in a similar way to iOS and have them hidden in the Apps own container.
Write them to getFilesDir(), getCacheDir(), or another location on internal storage.
This is what I have currently, does it do the trick?
The ContextWrapper is useless. If you are downloading an image, I do not know why you have a Bitmap that you are trying to write to storage — download straight to storage.
But, with respect to keeping the images private to your app, getDir() also points to locations in internal storage.
In my Android application, I save a bitmap file into a directory in the external storage by using the following codes:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
final String mPath = Environment.getExternalStorageDirectory().getAbsolutePath() + getResources().getString(R.string.record_folder);
File dir = new File(mPath);
if(!dir.exists()) {
dir.mkdirs();
}
OutputStream fout = null;
File imageFile = new File(mPath + getResources().getString(R.string.file_name));
Bitmap im = Bitmap.createBitmap(b, 0, 0, canvas.getWidth(), (int) ((float)canvas.getHeight())*600/800);
try {
fout = new FileOutputStream(imageFile);
im.compress(Bitmap.CompressFormat.JPEG, 97, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
// ?
}
If the device in which my application is installed doesn't have an external storage, what should I do?
Thanks.
Check the storage options which the android OS provides. If no external storage is mounted, I would save the file in the internal storage.
http://developer.android.com/guide/topics/data/data-storage.html
Then you can use:
Context.getFilesDir()
to get path to file in internal memory. If your device requires presence of external memory, then you can inform about it your user and ask if storing image in internal memory is OK.
I have a device without a memory card and I want to save the file. I've tried:
filePath = Environment.getDataDirectory().toString();
/data/.... EACCES (Permission denied)
filePath = Environment.getDownloadCacheDirectory().toString();
/cache/.... EACCES (Permission denied)
and
filePath = Environment.getRootDirectory().toString();
/system/.... EROFS (Read-only file system)
is there other way?? (Sorry for my english :P)
Try to write file into internal storage, in your apps local space, file will be written somewhere like , below code may help you in this regard
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write("yourString data");
osw.close();
fOut.close();
}catch(Exception e){
e.printStackTrace(System.err); }
you can try this. I m using this for storing download images
String cacheDir=
context.getCacheDir();
File file= new File(cacheDir,
"test.dat");
FileOutputStream out = new
FileOutputStream(file);
Save file in internal memory but you can not see the files which saved in internal memory.
Use this code to save the file in internal memory.
public boolean saveImageToInternalStorage(Bitmap image,Context context)
{
try {
FileOutputStream fos = context.openFileOutput("photo.jpg", Context.MODE_WORLD_READABLE);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// 100 means no compression, the lower you go, the stronger the compression
fos.close();
return true;
}
catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
return false;
}
Read this article to Save file in internal and external memory.