I'd like to open a file that I written copied to my cacheDir (e.g audio file, or image) with an intent, but it seems like other apps don't have the permission to this directory. It's quite reasonable (my logic tells me that only root and the app have the access to this folder), but is there any workaround for this issue?
Well, that's quite obvious my friend, use activity.getExternalCacheDir() to obtain a cache dir on your SD card. Just don't forget to set the permissions.
Related
I'm making an app which contents should be downloaded from a server. After downloading videos, I want to prevent them from publishing or sharing. I searched about this and find out that putting a dot (.) before the name of the video will hide it from file manager and gallery. But if the user uses a professional file manager would see the videos!
I want a solution to make videos unplayable via other apps.
As far as I Know, you could put your files in your internal directory which is private and only your app can access it.
but still, devices with root access can view and edit your files.
you can get private file location via context.getFilesDir()
You can use cache directory instead of External storage
Ex:
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
each app has own cached directory and can accessible only for that app (unless root)
That one simple way to hide media from other apps
more
I'm using the Android-Download-Manager-Pro library from GitHub for downloading files on android.
After the download is finished, in the logs I see this:
Create file address: /storage/emulated/0/Downloads/uHlNhjA7c_g_27.jpg
It's a path to a file, which was downloaded a moment ago. When I try to create the File value and access it, I get a NullPointerException:
File file = new File("/storage/emulated/0/Downloads/uHlNhjA7c_g_27.jpg");
When I look at the length of the file in logs, it's always 0. How do I correctly get access to the downloaded file?
Try this:
Environment.getExternalStorageDirectory(). Don't be confuse with the name of the function. Read this in the documentation:
Note: don't be confused by the word "external" here. This directory
can better be thought as media/shared storage. It is a filesystem that
can hold a relatively large amount of data and that is shared across
all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a
device that is distinct from the protected internal storage and can be
mounted as a filesystem on a computer.
i found the solution, i forgot to add permissions to manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
This adress could be the right one. Not sure for that:
File saveFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) , "uHlNhjA7c_g_27.jpg");
Acording to docs:
Standard directory in which to place files that have been downloaded by the user.
I can output the files and store then in a location on the device just fine with
File dir = new File(Environment.getExternalStorageDirectory(), "Army PRT Manager");
dir.mkdir();
File file = new File(dir, fileName);
I can view the files on /storage/emulated/0/Army PRT Manager
However, when I browse to that folder via my laptop to copy the created documents I can't find the folder.
All folder options have been changed to View hidden folders/files.
Is this a problem with many devices and can anyone suggest how I can save in a way that I would be able to see the documents more easily.
Maybe save them to the download folder?
Thanks.
Also when I open my device on my computer it only shows a Internal storage folder to open. I just want to be able to write to a directory that is easy to get to from a computer or file manager on the device. =(
What shows up via the MTP connection to your laptop is what MediaStore knows about. Use MediaScannerConnection and scanFile() to tell MediaStore about your file, after you have completely written it to disk.
Even with this, depending upon your laptop's OS, you may need to do some sort of "refresh" in a file manager to get it to pick up the new file, or possibly even unplug and re-plug in the device.
Use this code after dir.mkdir()
MediaScannerConnection.scanFile(this, new String[]{dir.toString()}, null, null);
I have small XML(KB) file in my assets folder. My application draws data from it. But then I have downloaded an updated version of the file I need to replace it somehow and I don't know how to?
Since I am developing on higher API`S I can use DownloadManager class.
You cannot replace an asset -- assets, like resources, are read-only.
Instead, you will need to adjust your application to detect if you have downloaded the replacement file and use that instead of the asset if it is available.
My solution is:before i use the assets file i delete all cache file int the path---/data/data/com.yourpackagename.xxx,ofcourse just when the app version is above the old app version.(i saved a SharedPreferences when the app first run.so i can compare the app version).
I don't know why the new app's assets resource is exist,but the solution is worked for me!
ps:my app homepage is a webview and the file in the assets,so,i need replace it each app update!
I'm trying to push one mp3 file to (Emulator) the location /system/media/audio/ui using command adb push But, i'm having
E:\Android\android-sdk\platform-tools>adb push song.mp3 /system/media/audio/ui
failed to copy 'song.mp3' to '/system/media/audio/ui/song.mp3': Read-only file system
this error. How can i change the Permission or how to push the files to this location. Any idea?
Unless you have a really good reason not to, you should stay in the /sdcard/ folder. Except for /sdcard/, most of the file system is read-only for a normal user.
It is possible, but not recommended, to "root" the phone. After which you would have full access to the whole system. Attempting this on a real phone, could break the phone, or invalidate your warranty.
If you wish to get root-privileges on the emulator, you can look at the accepted answer to this question: how to root/getroot access on android emulator?
That means your emulator does not support the sdcard. So create the emulator with sdcard option. see the bello image...