Im using a download manager which downloads a file into a new folder on the phone storage. Here is the code im using:
DownloadManager.Request downloadSample = new DownloadManager.Request(Uri.parse(urlSample));
downloadSample.allowScanningByMediaScanner();
downloadSample.setDestinationInExternalPublicDir("/Samples/"+previewName, "sample.ttf");
downloadSample.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(downloadSample);
This works perfectly fine on many of the devices Ive tested on, but a few devices force close the app with the following error in the log:
E/AndroidRuntime(29918): java.lang.IllegalStateException: Unable to create directory: /storage/sdcard0/Samples/Helvetica
E/AndroidRuntime(29918): at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:507)
Its annoying that it works fine on some but not at all on other devices. Does anyone know why this is happening?
The documentation for setDestinationInExternalPublicDir(String dirType, String subPath) says that dirType can take only specified values.
Here dirType can only be the android defined directories for example:
Environment.DIRECTORY_DOWNLOADS
etc.
you can find all the possible values of dirType at: http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
So the proper way to use this method is:
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath)
Also note that this will make the folder in Downloads folder on phone storage. Download manager will not allow you to make a folder in the default directory of phone storage.
This will not give you IllegalStateException for creating directory.
Related
I am trying to get access to a text file (log.txt) in a directory. All other questions on this topic refer to getting directories from the emulators internal storage.
My file structure is as such
>androidApp
->App
-->Build
-->src
--->game_log
---->log.txt
--->Main
---->(Android app Code further)
Using new File(System.getProperty("user.dir) + "app\\src\\game_log\\log.txt").exists() gets me false.
Another thing I tried was System.getProperty("user.dir") but that yields me /.
Contextwrapper.getPath() gets me the path of the emulators storage.
Is the file structure of Android Studio different or I am using the wrong method to get the file from my project folder?
I have an app that downloads mp3 files from a url. The downloading works, and I get a file in the downloads folder. However, when I click on it, it says "Can't open file." When I use getExternalStorageDirectory(), it also isn't retrieving the file or getting any of the information. Is there something wrong with how I'm implementing the download function? Or is there more I have to do with the download manager? I also tried using addCompletedDownload(), but it would just add a "0" file.
private long DownloadData (Uri uri) {
long downloadReference;
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
// Create request for android download manager
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Downloading Song");
//Setting description of request
request.setDescription("Downloading Song from URL");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(MainActivity.this, Environment.DIRECTORY_DOWNLOADS, "DownloadSong.mp3");
//Enqueue download and save into referenceId
downloadReference = downloadManager.enqueue(request);
return downloadReference;
}
The code that you had shown to us is absolutely not enough to understand the problem. Here is an example code for downloading using DownloadManager.
According to the symptoms described, I think, you haven't finished the download operation or the writing operation correctly. You should check if the download is finished and the file is written and closed. The fact that a file appeared, means that the downloading and writing had started, but not that they finished correctly.
Using DownloadManager terms, it should be completed. Look How to notify after all download completed from Android download manager about that
I would like my Android app to find the main directory on my tablet's internal storage. The furthest I can get is reading the contents of the "user.dir", but that seems to be the root directory and the listing there is too immense to find what would be considered the DOS equivalent of C:\
I am listing the code snippet I use to read the directory to show I can access the internal storage, but somewhere I'm not able to do it correctly.
workingDir = System.getProperty("user.dir");
File f = new File(workingDir);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
Thanks
in fact, if you were to open the "My Files" explorer that comes with Android the top option on my tablet is called Internal Storage, and it lists all the viewable folders from within
That is what the Android SDK refers to as external storage. You can find the root of that via Environment.getExternalStorageDirectory(). However:
Don't put things directly there, just as well-written Windows programs haven't put stuff in C: for the past couple of decades. Use Environment.getExternalStoragePublicDirectory() for standard locations (e.g., downloads directory), or use getExternalFilesDir() on Context for a location that is unique to your app.
You need to hold READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE permissions, including setting up runtime permissions for use on Android 6.0+ devices.
You can try to give a look at the android Environment documentation and access internal storage root/main directory with this Environment.getRootDirectory()
I need to create an application that will install some apk (the name is myapp.apk) file that I created. (I checked myapp.apk alone and it works fine)
I've created a folder raw inside the res folder and put the myapp.apk file there.
I added the following code to my application:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("R.raw.myapp")), "application/vnd.android.package-archive");
But I'm getting a parse error on the emulator (eclipse android emulator).
I appreciate your help
First, R.raw.myapp is not a file. It is an integer identifier of a raw resource.
Second, you cannot install an APK from a resource (or an asset, or a ContentProvider). It must be a file on the filesystem, readable by other processes.
So, use getResources().openRawResource() to get an InputStream on your APK, write it to external storage, then use the file on external storage in your Intent.
I am trying to save a file to my SDcard on my Samsung Galaxy Nexus running 4.2.2. From my app I am using
Environment.getExternalStorageDirectory()
But it returns
/storage/emulated/0/
Which is not where my SDcard information is located to. Is there any working method I can use to get the correct path to my SDcard?
Actually, that is the correct location.
From android 4,2 onwards, Google introduced multiple user accounts. Every user has his/her own external storage, which has the user ID in the path to maintain uniqueness.
The primary (default) user's ID is 0. So you get /storage/emulated/0/ as the path to the external storage.
I just learned this is the Jelly Bean's way of dealing with the lack of android.permission.WRITE_EXTERNAL_STORAGE permission. I haven't seen such a behavior with older versions of Android.
Just add this line to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". I can be something else than the SD-card.
You may get more informations here: Find an external SD card location
I think its because of using genymotion emulator,
the path is true and in Eclipse it locates at
File Explorer - > mnt - > shell - > emulated -> 0
hope it helps ;)
Sure? Where do you think your sdcard is mounted? Try doing ls -l on that directory – it's probably a symlink to /storage/emulated/0/.
I had somehow lost my symlink at /storage/emulated/0, causing a similar failure. What I did was use the camera app to take a picture. I observed that the pictures details said it was within /storage/emulated/0, though that directory did not exist from the file explorer. Then I rebooted the phone. After reboot the link existed.
Try to use getExternalFilesDir() or getExternalStoragePublicDirectory().
From Android 4.2 documentation