Make images and folder invisble to Gallery saved on SDcard? - java

Hi I want to make images invisible to android gallery or any third party gallery app, the image will be places in specific folder on SD card.
For example I have following code to save an image to a folder called myimages. I just want the images stored in myimages folder should not be visible to any gallery app and only my own application can access these images.
void saveBitmap(Bitmap bmp)
{
FileOutputStream os;
String dirName = "/mvc/mvc/myiamges/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
String root = Environment.getExternalStorageDirectory()
.toString();
File dir = new File (root + dirName);
boolean created=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, "aeg2.png");
os = new FileOutputStream(file);
}else{
os = openFileOutput("aeg2.png", MODE_PRIVATE);
}
bmp.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
}

Rename those files with custom extensions like filename.extension.customextension
like hello.avi.topsecret.
When you need the file to be ready mode to play rename it to proper extension, play and rename it back.
This should work for you.
or
Prefix your folder name with a dot "."
Check these links for more info:
http://www.makeuseof.com/tag/hide-private-picture-folders-gallery-android/

Yes, save it with any extension you want or even without extension.
In your app, just read it as normal image file.

Create an empty file inside your image store folder named '.nomedia' <- atention to the initial point.
All media files sabed inside this folder will not be showed in galery browsers.

Related

Where should I save my file in Android for local access?

I have two datasets which are currently in the same folder as my java files AND on my PC. Currently, I am accessing them through my C-drive. Since this is an app, where should I save my .ARFF files and what path should I use instead? I have tried in the raw folder, but nothing seems to work.
Here's what I have so far...
Create a raw directory in your project, raw is included in the res folder of android project. You can add an assets files in raw folder like music files, database files or text files or some other files which you need to access directly
1) Right click on res folder, select New> Directory, then studio will open a dialog box and it will ask you to enter the name.
2) Enter “raw” and click OK. Open res folder and you will find your raw folder under it.
InputStream input = Context.getResources().openRawResource(R.raw.your_file_name);
// Example to read file from raw directory
private String readFileFromRawDirectory(int resourceId)
{
InputStream iStream = context.getResources().openRawResource(resourceId);
ByteArrayOutputStream byteStream = null;
try {
byte[] buffer = new byte[iStream.available()];
iStream.read(buffer);
byteStream = new ByteArrayOutputStream();
byteStream.write(buffer);
byteStream.close();
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteStream.toString();
}
}
After too many hours
A very easy solution to retrieving data from the assets folder! Only one user-defined method.
Make raw folder in res directory.
Paste whatever files in the raw directory
Make a separate .java file
Make sure it is a derivative class (in this case it extended AppCompatActivity
Write Part A in the body
Write Part B outside the body
A. This is in the main function OR in a custom, user-defined function.
BufferedReader bReader;
bReader = new BufferedReader(
new InputStreamReader(ISR(R.raw.FILENAME_WITHOUT_TYPE)));
FILENAME_WITHOUT_TYPE refers to only the name of the file, not its ending (everything followed by the .).
B. This is the definition of ISR.
public InputStream ISR(int resourceId) {
InputStream iStream = getBaseContext().getResources().openRawResource(resourceId);
return iStream;
}
Works like a charm!
Resources:
https://inducesmile.com/android-programming/how-to-read-a-file-from-raw-directory-in-android/
https://gist.github.com/Airfixed/799e784696b0a60c5423d347bf33a341

How to save a .GIF file into the gallery?

Why is this so difficult to do in android? I know its easy for images, but why not .gifs?
I have this code here which saves it to an SD card, but I am trying to account for the user not having an SD card.
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "GIFName_" + System.currentTimeMillis() +".gif");
try {
FileOutputStream f = new FileOutputStream(file);
f.write(generateGIF(list));
} catch(Exception e) {
e.printStackTrace();
}
My app basically converts images to .GIFS, and right now it saves it on the sd card, but I want to save it to the gallery. Is there any way to do this easily? I know you can do it for images, but can you for .GIFS that are created?
What exactly do you mean by Gallery? There is no directory called Gallery. However there is an application called Gallery and I hope that's what you mean.
Environment.getExternalStorageDirectory() will return the root path to the external storage. This has no dependency to the file you are trying to save. If you want save to the Pictures directory, then you can do Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Gallery is an application in android that scans the whole system and adds all media items to it. If you try saving a file, be it .gif or .jpg, or .png, programmatically in Android, there is no guarantee that the file will be picked up by Gallery immediately. That's why you need to use MediaScannerConnection. This will let you add your newly created file to be shown in Gallery.
Something like below:
MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
Documentation: https://developer.android.com/reference/android/media/MediaScannerConnection.html#scanFile(java.lang.String, java.lang.String)

folder is created in eclipse home not in web application

I have create folder (i.e uploads ) in web application. I want to create one more folder inside "uploads" folder at runtime depends one the username of user. for this i have write below code. This code is creating folder and file but the location is different that i expected.
the location that i am getting is in eclipse location not web application location
D:\PAST\RequiredPlugins\JUNO\eclipse\uploads\datto\adhar.PNG
then i am getting error in FileOutStream that "system can't find the location specified."
public String getFolderName(String folderName, MultipartFile uploadPhoto)
throws ShareMeException {
File uploadfFile = null;
try {
File file = new File("uploads\\" + folderName);
if (!file.exists()) {
file.mkdir();
}
uploadfFile = new File(file.getAbsoluteFile()
+ "\\"+uploadPhoto.getOriginalFilename());
if (uploadfFile.exists()) {
throw new ShareMeException(
"file already exist please rename it");
} else {
uploadfFile.createNewFile();
FileOutputStream fout = new FileOutputStream(uploadfFile);
fout.write(uploadPhoto.getBytes());
fout.flush();
fout.close();
}
} catch (IOException e) {
throw new ShareMeException(e.getMessage());
}
return uploadfFile.getAbsolutePath();
}
i want to save uploaded file in web app "uploads" folder
Your filename is not absolute: uploads\folderName is resolved against the current directory, which the Eclipse launcher sets to JUNO\eclipse.
You should introduce an application variable like APP_HOME and resolve any data directory (including upload) against this variable.
Also, I suggest not to name anything (neither files nor directories) on your filesystem after user-entered input: you are asking for troubles (unicode characters in the user name) and especially security holes (even in combination with the unicode thing). If you really want to use the filesystem, keep the filename anonymous (1.data, 2.data, ...) and keep metadata inside some database.
You can do something on below lines in your webapp:-
String folderPath= request.getServletContext().getRealPath("/");
File file = new File (folderPath+"upload");
file.mkdir();

Save image to iOS Gallery from libGDX

I need to export an image from a libGDX game, and make it appear in the default Photos app on an iPad.
Currently, I do it like this:
Pixmap image = getScreenshot();
FileHandle file;
String filename = "diplom_" + game.player.getID() + ".png";
if(Gdx.files.isExternalStorageAvailable())
file = Gdx.files.external(filename);
else
file = Gdx.files.local(filename);
PixmapIO.writePNG(file, image);
pixmap.dispose();
But the screenshot doesn't appear anywhere. How can I make it appear in the Photos app?
What I am doing in such case is:
private NSData getImageAsNsData(Pixmap pixmap) {
FileHandle file = Gdx.files.local("tmpImage.png");
PixmapIO.writePNG(file, pixmap);
NSData imageData = NSData.read(file.file());
file.delete();
return imageData;
}
public void sendToGallery(Pixmap pixmap) {
NSData imageData = getImageAsNsData(pixmap);
uiImage = new UIImage(imageData);
uiImage.saveToPhotosAlbum(new VoidBlock2<UIImage, NSError>() {
#Override
public void invoke(UIImage uiImage, NSError nsError) {
if (nsError!=null)
Gdx.app.log("Error", "Unable to save: " + nsError.getLocalizedDescription());
}
});
}
I hope it helps you. :)
At first try adding logger so you see where you add put the file.
if you put it in local:
Local files are stored relative to the application's root or working directory on desktops and relative to the internal (private) storage of the application on Android. Note that Local and internal are mostly the same on the desktop.
if external
External files paths are relative to the SD card root on Android and to the home directory of the current user on desktop systems.
refare to FileHandling libGDX wiki
So i guess you save it in local thats why you wont find it in iOS. Else you need to create the right path to the pictures folder. Else you just save it at the same folder where the apps get installed. But i think youll need to use the absolut verion of the filehandle. Else you can save something at an total different path.
In this case, “myfile.txt” needs to be in the users’ home directory (/home//myfile.txt on linux or \Users\\myfile.txt on Windows and MacOS) on desktop, and in the root of the SD card on Android.
FileHandle handle = Gdx.files.absolute("/some_dir/subdir/myfile.txt");

How to set file path to src folder of project

I want to make a program that you can email to someone and they can run it.
Right now my code for making a file is like this:
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();
But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?
Leave the path off entirely, it will use the directory of the project.
Change
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");
To
File f = new File("axmlfile.xml");
I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.
try{
//create a temporary file
File temp = File.createTempFile("axmlfile", ".xml");
System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}

Categories

Resources