application failed to create folder when other application occupy it - android

This is my question.
First I have create my personal folder on create
File parentFolder = new File(MainApplication.getInstance().getExternalCacheDir().getAbsolutePath()
+ File.separator+"myfolder");
if (!parentFolder.exists()) {
parentFolder.mkdirs();
}
Second in my application,i can receive file such as png from another application,and the file that i received has been saved in /Android/data/packageName/cache/myfolder/hashcode/example.png. And i choose the gallery to open it from intent chooser.
When gallery is open and I can see the png file.I kill my application's process and uninstall it.
Finally I install my application again.The path /Android/data/packageName doesn't been created! And create function show that
MainApplication.getInstance().getExternalCacheDir();
returns null!
Give me some advise! Thank you.

Try using
new Context().getApplicationContext().getExternalCacheDir().getAbsolutePath()

Related

FileReader is not able to read a file from camera - Xamarin + WebView

I have an APP that is only a container for a VUE PWA. In one page the user could upload some file. I implemented the following solution to give the user the option to select a file from Galery or take a new photo.
https://blog.verslu.is/xamarin/xamarin-forms-xamarin/building-a-hybrid-app-with-xamarin-forms/
The problem is the FileReader is not able to read the file when the user takes a new photo with the camera. if the user selects an existing file, the upload works well. This is the error:
FileReader:
message: "The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired."
name: "NotReadableError"
I noticed one difference: when the user selects a file from galery, the URI path that the APP sent to the WebView looks like the following:
{content://com.android.providers.media.documents/document/image%3A32681}
When the user takes a new photo:
file:///storage/emulated/0/Pictures/JBSTRIP/IMG_637273232207442660.jpg
Anyone had the same problem? I didn't found any solution from web.
Thanks a lot!
I solved the problem.
I changed the directory where the picture from camera is saved:
Instead get the path using:
File imageStorageDir = new File (Android.OS.Environment.GetExternalStoragePublicDirectory (
Android.OS.Environment.DirectoryPictures), "APP");
I used the APP's folder:
File imageStorageDir = new File (_context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures), "APP");

Open an Image in Internal Storage Using Content Provider

Let's say I package an image with my app and I want to open it with the default image viewer/whatever image viewer the user has chosen to be the default. How would I do that?
There's already this post: Open an image using URI in Android's default gallery image viewer but many of the answers are obsolete because due to the introduction of android N, a content provider must be used.
The only answer I can find is this one:
File file = ...;
final Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
but, according to the author of this solution, this code only works for images stored externally, not ones that may be packaged with my app.
You won't be able to open an image packaged with the app (in drawable or whatever resources) through an external application. You should first copy it into (at least) an internal file storage. After that you can implement a FileProvider to provide access to this file.
Let me know if you need more details on this. Hope it helped.

how to hide images from android gallery

One part of my current project is downloading images from URL then saving to SDCard. But the problem is all of saved images in sdcard is displayed in android gallery page. What I want is "I don't want all of my saved images in android gallery."
Add ".nomedia" file. Did not work? Try the second option.
Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/
You can create .nomedia file using this code:
String NOMEDIA=".nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
I found a simple way(I think so).
Create a folder in 'Android' folder(where data & obb folders are present) and put your media(pics,videos, etc.,) in the folder you've created.
Gallery app ignores that 'Android' folder. Simple :-)
its so late and i know other answers are complete but i think putting .nomedia file makes all pictures in that particular folder hidden :
just change extension of image file programmatically and this way gallery cant detect it as an image . eg if your image is "pic1.jpg" rename it to "pic1.aaa";
and when you want show it again rename it again

Make picture available only for my Android application

I have an application which has loads of pictures. The total picture is saved in an SD card due to large number of picture. The main problem is that the pictures are available in gallery. I want a way to hide the picture from the gallery but must be available for my application.
I did put " . " in front of the file name to make it hidden
but the images were not displayed in may application also!
Anyone.. help.. please
thanks..
Gallery handles all media files it knows about, so if you want it to stay away of your assets you got two solutions. First, add .nomedia file to the folder with your images, and Gallery should skip it (most image viewers would honor .nomedia, yet there's no guarantee). Other solution is to use some own, proprietary file format, so Gallery or other tools won't be able to process it. I'd recomment .nomedia if that's sufficient solution.
Add .nomedia file into the image folder. The important thing is that, the folder should be empty, when you add .nomedia file. If the folder contains some images before adding '.nomedia' file, the gallery will not hide those images, and it will hide the images added after '.nomedia' file. So The first file added to the image folder should be '.nomedia'.
Hope dis will help you
Thankx,
This line of code can add the .nomedia file in your directory and makes your images hidden from the gallery
String NOMEDIA=" .nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
i think the place where you save the images matter, try to save them into application cache directory, or files directory.
you can get access to cache directory using : getCacheDir() in your activity.
and your files directory: getFilesDir()
these two directories must not be visible or accessible to any other applications expect your application,
I used assets to accomplish this in my app.

Create Directory when my application installed first time

I want a develop simple app in which i have to check at the time of my app installation that directory that i create in my app is already exist before the app is installed, if yes then i have to remove that directory and create new directory in the sdcard.
I am able to do it at installation time but the actual problem is that i put that code in my main activity so when i reopen my app at that time directory is deleted also , so that i don't wont to do because i have put data on that after first installation .
If anyone know the way then please tell me ..
Kind regards,
Jalp.
File dir = new File("/sdcard/dirname/");
if(!(dir.isDirectory() || dir.exists())) dir.mkdirs();
File wallpaperDirectory = new File("/sdcard/dirname/");
// have the object build the directory structure, if
// needed.
if(!wallpaperDirectory.exists())
{ wallpaperDirectory.mkdirs();}
please try this

Categories

Resources