Open a file from SD card in android - android

To open a file from android application asset folder I used below code:
InputStream fis = getApplicationContext().getAssets().open(filename);
Same thing I want to open a file from SDcard... How to do?

File file = new File(path);
InputStream in = new FileInputStream(file);
This should work as long as the sdcard is mounted with a valid path.

Related

Android - Write file to assets folder

I want to download some data and store it into a file. I am aware that I cannot write to the assets dir or any other place because of the sole nature of the APK file.
I am wondering, if I create a file with something like:
FileOutputStream fileout = openFileOutput("text.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
outputWriter.write("write this string to file.");
outputWriter.close();
Since I cannot write any files to assets or any raw dir, where it will be written?
What will the location of that file be? Where will it be located in the Android file system ?
Since I cannot write any files to assets or any raw dir, where it will be written?
openFileOutput() writes to internal storage.
What will the location of that file be? Where will it be located in the Android file system ?
That can vary by device and user. It will be to the same directory that is returned via a call to getFilesDir().
You can get the path to your file created with openFileOutput like this: MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt" and you can read it using an object of FileInputStream FileInputStream fis = new FileInputStream(new File(MainActivity.this.getFilesDir().getAbsolutePath()+"/text.txt"));

Unable to read file from the Downloads folder for an android app

I want to create an inputstream for a file that is stored in the Downloads folder of my phone.
Earlier I had the required file as an asset and I read it with this code:
InputStream fin;
fin = getAssets().open(FILENAME);
However now I want to be able to open a file stored in my downloads folder /mnt/sdcard/Download as an InputStream NOT as a FileInputStream Object. I am using a library which requires an InputStream only as an argument so I can not use the FileInputStream
methods because I get a fatal exception if I do and the App stops.
How do I go about this? I tried to find some helper function from the documentation, but could not.
Edit: I also tried editing the code to this-
String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));
However still when I try to read the file, the App crashes.
I had the same issue as yours :
AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Here I used to access file1.xls from the assets folder.
I fixed it by using :
File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls"); // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Don't forget to get the storage permission first to access the storage.

Android creating text file

I want to create/write new files on the device.
I am able to create file on sd card of the android device.
But in case if device is not having sd card then where should i create file which can be accessible from the ddms.
I tried to create it in /data/data/com.abc.app/files/ directory, But i don't have permissions to access this directory from the ddms to save the file on pc.
Which is the best location create new file on the device which can be access from ddms and application.
Try use cache dir local and external.
Here is a part of my code
File dir = context.getExternalCacheDir() != null ?
context.getExternalCacheDir() : context.getCacheDir();
File file = new File(dir, ".cachefile");//hidden
if (file.exists())
file.delete();
FileOutputStream out = new FileOutputStream(file);
//..
out.flush();
out.close();
Don't forget to have permissions read/write;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
you have the permission to write/read in/from that folder through your applicatin. You have to use the pair openFileInput/openFileOutput
here the doc per openFileInput/openFileOutput

How to make folders of application in SD card are not accessible by file manager and other applications?.

I am creating two folders to store images of my application in SD card.but these folder are visible in file manger.i want to prevent access of my folder from outside.i am saving images in that two folders.i am saving like this please help me any one.
File sdcard = Environment.getExternalStorageDirectory();
File pictureDir = new File(sdcard, "Image_dir");
if (!pictureDir.exists()) {
pictureDir.mkdirs();
}
// saving the image file in the folder Image_dir
File f = null;
f = new File(pictureDir, file_name);
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
Bitmap image;
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
normally adding a . in front of the name of the folder which will exclude it from media scanners. But with a file manager can still find it.
If you are working on Api Level-8 then I suggest to use getExternalFilesDir (String type)
instead of Environment.getExternalStorageDirectory() because there are two benefit using this
all images and data will be automatically deleted when your application uninstall by user
images do not visible in the media application (in Gallery).

save image to sdcard android Directory problem

Im trying to save data to sdCard first i tried to saave it privately within app directory on externalStorage using getExternalFilesDir but gives me nullPointerException so i tried the other way given below it worked but when i want to store files into a custom directory that i want to named myself it give me error:
FileOutputStream os;
dirName = "/mydirectory/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + dirName);
dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
ErrorLog:
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/myfile2.png (No such file or directory)
Your directory "/mnt/sdcard/mvc/mvc" may not exist. What about changing your path to store the image in the Environment.getExternalStorageDirectory() path and then working from there?
Also, as Robert pointed out, make sure you have write permission to external storage in your manifest.
Edit - to create directories:
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/mvc/mvc").mkdirs();
Then you can save a file to root + "/mvc/mvc/foo.png".
Have you requested permission to write onto SD card? Add the following string to you app manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should check if you have added the required permission android.permission-group.STORAGE to your app. Without that permission you won't be able to access anything on the SD-Card.
BTW: On the Android system I know the SD-card is mounted on /sdcard not /mnt/sdcard
I found this book to be very helpful: "Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets". I noticed a part that allows saving images and stuff to the SD card.

Categories

Resources