Android doesn't find file - android

I've a file in my Download folder called random.txt and I want to display this in my android app. So I thought I could write this code to get the file path and open it:
String path= Environment.getExternalStorageDirectory()
.toString() + File.separator;
openPdfIntent(path + "/Download/random.pdf");
But I get the log message that my file doesn't exist. If I browse to the location with my file manager the file is there.
My phone is a HTC one so I don't have a external sdcard.

Remove File.separator from 'path' variable.

I forgot to give permission in my manifest.
That in combination with
String pdfFilePath = Environment.getExternalStorageDirectory()
.toString() + "/Download/random.pdf";

try this:
pdfFilePath = "file://" + Environment.getExternalStorageDirectory()
.toString() + "/Download/random.pdf";

Related

get external sdcard specific folder path for all android devices

Is there any common way to find the specific folder in micro SD card path all crevices?
how to get this path by something like
/storage/extSdCard/MYFOLDER
This is my code its only working on android 4.4.
String securepath = secStoreSystem.getenv("SECONDARY_STORAGE");
String defaul_directory_tpath = secStore+"/MYFOLDER";
secStoreSystem.getenv("SECONDARY_STORAGE")
getting null value in android 4.4+ devices
try this
path = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
//getting your file/folder
File f = new File(path + File.separator + fileName);
As per your code you are saving external storage path to string variable path
but your are using path1 for getting your folder. Please check this.

Get android app sandbox path

My app downloads image files for offline use. Using the DownloadManager, the files are saved to the app's /data/ folder (retrieved with Environment.getDataDirectory().getAbsolutePath()). When displaying the images, the function File.exists for the path /data/filename returns false. When I provide the full path (kinda harcoded with the following function) everything works.
private static String getImageUri(Context c, String name) {
return Environment.getExternalStorageDirectory().getPath() + File.separator +
"Android" + File.separator + "data" + File.separator +
c.getPackageName() + File.separator +
"files" + File.separator + name;
}
It seems the File api requires the full path but I couldn't find a more elegant way of getting the full path.
Is there a better way of doing this? Another api that allows r/w and treats paths the same way the DownloadManager does? Or a better way of retrieving the full path?
The code returns the path storage/emulated/0/android/data/com.package.app/files/data.
I assume that your lowercase android is a typo, as that location should be Android.
You can obtain that location more simply via getExternalFilesDir() (method on Context), though you will need to append the trailing data segment yourself.
The line request.setDestinationInExternalFilesDir(context, Environment.getDataDirectory().getAbsolutePath(), name); works
It would be simpler — and far more compliant with the documentation — to just use request.setDestnationInExternalFilesDir(context, null, name). Then, your downloaded file would be in the location new File(getExternalFilesDir(null), name).

IOException: No Such File or Directory (Android)

This is my first Android app and my first attempt at writing something to file. I'm trying to capture a log according to these instructions and I'm getting the FileNotFoundExeption ENOENT (No such file or directory). That's fair enough because the directory does not exist. But then how do I create the directory? Or use another one? I don't know best practices for where to write logs to email them, nor do I know how to make a new directory for them.
This is the path I'm trying to use.
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
String fullName = path + "mylog";
File file = new File (fullName);
The parent dir doesn't exist yet, you must create the parent first before creating the file
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
// Create the parent path
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fullName = path + "mylog";
File file = new File (fullName);
Edit:
Thanks to Jonathans answer, this code sample is more correct. It uses the exists() method.
You also need the permission in your manifest:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
I'd like to add to Francesco's answer, that instead of asking if it's a directory, you could ask if it exists with dir.exists() method.
And also check that you've set the proper permissions in the Manifest file.
Hope it helps
Jonatan

Should I save external data in directory Android/data or data/?

Which is correct,
String filePath = Environment.getExternalStorageDirectory()
+ "/data/com.packagename";
or
String filePath = Environment.getExternalStorageDirectory()
+ "/Android/data/com.packagename";
if I want to store data in external storage? I see many apps are using the second option, but some use the first path.
You should rely on the API to figure out the directory for you:
File externalDir = Context.getExternalFilesDir(null);
Context.getExternalFilesDir will return your 2nd path. Programs that return the 1st path probably hardcoded the path and got it wrong as a result.

Saving data on external storage

I want to save some data in the user's external directory (ie. SD card), but there seems to be a weird problem. I'm using Environment.getExternalStorageDirectory() which returns "mnt/sdcard/" (which is fine). I want to create two folders on in this directory so I do:
File main = new File(getExternalStorageDirectory() + "/my_app/some_data");
if(!main.isDirectory())
main.mkdirs();
Now I thought this would make the directory "mnt/sdcard/my_app/some_data", but after using a file manager to look at the SD card, it turns out that this folder is created at "mnt/sdcard/my_app/mnt/sdcard/my_app/some_data", which is quite bizarre. Can anyone tell me how to fix this?
Try the following and see what you get...
String packageName = this.getPackageName();
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "files");
myFilesDir.mkdirs();
It's exacly what I use to create a working directory on an SD card. For me it creates...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
...where 'com.mycompany.myApp' is the actual package name of my app.

Categories

Resources