How to save file without SD Card? - android

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.

Related

Got FileNotFoundException for Environment.getExternalStorageDirectory()

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;
}

Create a file on Android Internal Storage?

I'm trying to write a text file in android's internal storage and browse through connecting it to a PC via usb. Now, I used the following code which worked fine for old devices with SD cards.
public void writeToFile(String stringToBeWritten)
{
final File path =
Environment.getExternalStoragePublicDirectory
(
Environment.DIRECTORY_DCIM + "/Target Folder/"
);
if(!path.exists())
{
path.mkdirs();
}
final File file = new File(path, "MyFile.txt");
try
{
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(stringToBeWritten);
System.out.println("Written Successfully");
myOutWriter.close();
fOut.flush();
fOut.close();
}
catch (IOException e)
{
Log.e("Exception", "File write failed: " + e.toString());
}
}
I've been surfing for a solution a while and tried a lot of thing to get my job done. All the solutions out there, to store the file internally but unable to browse.
Any ideas how to write my files on internal phone storage? I'd highly appreciate any help/suggestions. Thanks.

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.

If there isn't any external 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.

How to get an Uri on a file saved in app private storage?

I have to write an image in the private storage of my app. I do that this way:
FileOutputStream fos = openFileOutput(FILE_FRONT_PROCESSED_IMAGE_CACHE, Context.MODE_PRIVATE);
mFrontBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
My question is : Is it possible to get / forge an Uri pointing to this file ? (i found many solutions with external storage, but impossible to find out how to achieve this with private app storage)
EDIT (with CommonsWare advices) :
private Uri saveTempImage(Bitmap bitmap, String filename)
{
File cache = new File(getFilesDir(), filename);
try
{
FileOutputStream fos = new FileOutputStream(cache);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return Uri.fromFile(cache);
}
Is it possible to get / forge an Uri pointing to this file ?
If you use FileProvider, you can get a content:// Uri for a file.

Categories

Resources