If there isn't any external storage - android

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.

Related

Save bitmap to internal storage and then choose it with picker

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.

Writing to File Internal Storage Android

Given this code:
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/";
String file = dir + "info.txt";
File newfile = new File(file);
//Environment.getExternalStorageDirectory().toString()= /storage/emulated/0
String msgData="p1000";
FileOutputStream outputStream;
try {
newfile.createNewFile();
outputStream = openFileOutput(file, Context.MODE_PRIVATE);
outputStream.write(msgData.getBytes());
outputStream.close();
} catch (Exception e) {
e.toString();
//e.printStackTrace();
}
When I open file /storage/emulated/0/Documents/info.text, I find it empty while it should have the string "p1000";
why is that?
note: I do not have external storage (external sd card).
thanks.
Do you have WRITE_EXTERNAL_STORAGE Permission in your Manifest?
Try this: FileOutputStream outputStream = new FileOutputStream(file);
If you wanna use internal storage you should not pass a path pointing at the external storage (roughly sd card). In you try-catch block use this line instead:
outputStream = openFileOutput("info.txt", Context.MODE_PRIVATE);
The file will be saved in the directory returned by Context.getFilesDir().

Android, write image into internal memory

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.

How to save file without SD Card?

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.

StreamCorruptedException when reading Bitmap from SD card

I have a web service that returns a jpg. I read this jpg into a byte[], convert to a Bitmap, and save it to the SD card. The next time the user comes to this Activity, it will search the SD card to see if the image exists before hitting the web service.
However, the code that checks the SD card returns a StreamCorruptedException if the file exists.
Here is my code that writes to the SD card:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/images").mkdirs();
try {
File file = new File(root + "/images", Integer.toString(intImageId) + "m.jpg");
FileOutputStream os = new FileOutputStream(file);
Bitmap theImageFromByteArray = BitmapFactory.decodeByteArray(image, 0, image.length);
theImageFromByteArray.compress(Bitmap.CompressFormat.JPEG, 80, os);
os.flush();
os.close();
}
catch (FileNotFoundException e) {
}
catch (IOException e) {
}
}
Here is my code that checks the SD card for the existing image:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/images", Integer.toString(mImageId) + "m.jpg");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
mImage = (Bitmap)ois.readObject();
ois.close();
}
catch (Exception e) {
}
}
The exception happens during new ObjectInputStream(fis)
You cannot arbitrarily readObject() like this. writeObject() needs to be used in order for readObject() to detect a valid serialized object.

Categories

Resources