I want to insert images into my SDCard.So I used below code
m_cImagePath = "/sdcard/"+ String.format("%d.jpg", System.currentTimeMillis());
FileOutputStream lObjOutStream = null;
try {
lObjOutStream = new FileOutputStream(m_cImagePath);
if (null != lObjOutStream && null != finalBitmap) {
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 85, lObjOutStream);
lObjOutStream.close();
}catch(FileNotFoundException fe){
fe.printStackTrace();
}
Sometimes it is giving FileNotFoundException even my SDCard had memory.When I remove some images from sdcard again it is working smoothly.Why this Happend?How can i know that file is inserted successfully in SDCard and Is there any functionality in Java1.5 to know available space of the SDCard like java 1.6?How can i know file length which is not before inserting into the SDCard(I searched in google and found that
"when the file is not physically there
then file.length() always gives 0"
).But before inserting i want to know the length of the file.Then Comparing this space to available SDCard space is simple.
Note :I had an idea to use Unix command
df sdcard
using in
Runtime class
to found SDCard space.
Please give me an idea in this problem.
Regards,
Android Developer
Never never never never never hardcode /sdcard in an Android application. First, it's wrong on Android 2.2+. Second, it's wrong on other devices as well. Always use Environment.getExternalStorageDirectory() for the root of external storage.
Is there any functionality in Java1.5 to know available space of the SDCard like java 1.6?
android.os.StatFs has what you need.
Related
I have a question about Android programming. Basically, I am unsure of where to check where my file is, and if I wrote to it correctly. I want to locate where the file is, and I also want to know whether or not I wrote to it correctly. Below is the code I have come up with:
String lsNow = "testing";
try {
fos = openFileOutput("output.txt", Context.MODE_APPEND);
fos.write(lsNow.getBytes());
fos.close();
}
catch{
...
}
Where can I find output.txt? Might anyone know how to check this all out? if so, that would be great! I am using an emulator by the way. If I were to do this on a real Android, how would one approach this also? (Just for future reference)
You Test it in Two ways
Using File Explorer
Go to DDMS perspective--> Open File Explorer-->location of the file
Pragrammatically by using exits() method
File file = new File(context.getFilesDir(), filename);
if(file.exists())
Using openFileOutput(...) means the file will be written to internal storage on the Android device in an area which is secure from access by other apps.
If you want to make sure the file is written correctly then make sure your catch block handles any failures (if it is called then the file writing has failed).
To access the file once it has been written use openFileInput(...).
I have a problem till now I can not understand.
os = new FileOutputStream(file,doAppend);
The file is on SD. If doAppend is false the file is saved correctly. If I split file in blocks and use first write with doAppend=false and successive with true the file is not saved correctly (as it is truncated).
The same function saves also to private folder on device but uses just os = activity.openFileOutput(red.m_slika_ime, Context.MODE_PRIVATE+Context.MODE_APPEND);
Between saving to SD or private the only difference is in OutputStream instantiation.
Since on private folder all works as expected it should be the FileOutputStream with append on Sd that is not working correctly.
Is this a known issue? workaround?
Thanks
It was my stupid error. I was elsewhere, each time before writing to SD, erasing my own file.
Happens:)
Thanks Luke anyway
I want to read text file, that was written by other my app. It's saved ad "Android/data/MyPackageName/files/"
I use this code:
File file = new File("//Android//data//MyPackageName//files//", "filename.txt");
FileInputStream is = new FileInputStream(file);
but i get exception "no such file or directory"
I am sure that solution is pretty simple, but i can't find it yet.
Thank you for your help!
I don't think it is right to use the double backslash "//", one is enough. Also, the path should be "/mnt/sdcard/Android/data....". I am not sure the "/mnt/sdcard" is applicable on every device, so my suggestion is to use Environment.getExternalStorageDirectory to get the root dir on sd card.
I've researched this problem for a while now and I've only found really complicated answers so I'm very confused. Keep in mind that I'm not an expert programmer so don't expect me to know a ton about this!
All I want to do is print a new line of characters to a text file located in the downloads folder of an SD card in Android. I set up my emulator to have an SD card and placed the text file in the downloads folder. This piece of code is for a database class that will access a text file in an SD card to read the data. I know that the class works outside of Android so assume that all of the methods are working as they should to read the data!
I get an IOException when I run this method in another class:
public void addRecordToDataBase(ChildRecord c) throws IOException
{
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
PrintWriter out = new PrintWriter(outFile);
out.println(c.printToDataBase());
out.close();
}
The weird thing is I can read from the database just fine in other methods using that same path; no problems there. I just can't write to it. I've read somewhere that you can use "regular Java methods" to write to an SD card in Android without those crazy "OutputStream" things all over the place. Is this true? I debugged this thing and found out that the line of code that is throwing the exception is right here:
FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt");
If anyone has any idea why I'm geting this IOException, I would be really grateful! I did try all the crazy methods that Android wants to use but I think I got lost in it so I just reverted back to what I knew how to do.
Thank you so much!
My guess is you can't access such directory with writing permissions, at least in that manner.
Did you take a look at http://developer.android.com/guide/topics/data/data-storage.html#filesExternal?
That reference and this one (http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)), explaining how getExternalFilesDir works, may be of help to you!
Have you declared the WRITE_EXTERNAL_PERMISSION permission in your apps manifest file?
I am using this code to save a bitmap image taken from a screen shot:
try{
String fname = "/Pictures/YourApp/YourPic.jpg";
File path= Environment.getExternalStorageDirectory();
File file = new File(path, fname);
if (file.exists()) {
Bitmap bitty = BitmapFactory.decodeFile(file.toString());
yourPlan.setImageBitmap(bitty); //ImageView
}else{
errorText.setText("If you can not see your image then free up some memory");
}
}catch(Exception e){
errorText.setText("If you can not see your image then free up some memory");
}
It works fine on my testing phone - an Orange San Francisco on 2.1 but I have had emails from users telling me they only get the error message when saving with enough space.
They are using HTC and Samsung handsets. Any ideas?
First, I would like to point out that catching a general Exception e is not in good coding practice. Second, both messages you output to the user are also not helpful as it is not guaranteed that the error is space-related.
I am guessing the error comes from the file not existing. Where are you saving the file? From the snippet of code, I see that you are simply assuming the file exists. If you haven't yet saved the file after taking your screen shot, that would be your problem right there. You are opening a file that doesn't exist and your error message is giving the mistaken impression that there isn't enough space.