How to save a .GIF file into the gallery? - java

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)

Related

Creating a text file in android

so i know there are alot of questions about creating a text file in android but i cant find any answers to my specific questions.
So the device I'm using is not an phone or a tablet, its a controller with android on it so the file directory is getting me confused. i want to create a new file either on the sd card or the device itself (it doesn't really matter as long as i can see it). firstly, the device doesn't have google docs or sheets or anything like that, will i need to install one of these apps on it for it to be able to read .txt files? or does android have some sort of internal software to do that?
Secondly, i've found alot of code on how to create a new file but most of it seems to gloss over how to get file directory, is there a specific way i can get the file directory?
This is what im using so far and im not getting any errors but its also not creating any files (or at-least none that i can see).
public void WriteToText(){
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
1, I think all devices have Html viewer which can view the text files. If you click a text file in a file manager app, it should prompt you with a list of apps that can view the text file.
2, Your code is correct, but it creates a file in the app's private file directory, which is not visible to all other apps, including file manager apps. This is because of Android security policy to prevent any app to steal information from other apps.
If you want to write a file which is visible to other apps like file manager, there are 2 ways.
in old api level (<29?) you can get the get external storage with
Environment.getExternalStorageDirectory()
you will need to request permission for it.
save the file in your app's private directory like what you have done, then create a chooser to share it out with correct mime type (txt/plain?), it will prompt you to select app that can view this mime type like what file manager does.

Save file to internal storage in Android

I want to make a simple Android application which will make user write text and when he presses on save button I'll get the text from text area and create folder then save file into folder in the storage.
I did that but in external storage and here's my code
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfolder";
File f = new File(path);
f.mkdir();
PrintWriter pw = new PrintWriter(path + "/" + txt_name.getText() + ".txt");
pw.write(main_txt.getText().toString());
pw.close();
This code runs well but it runs only on devices which have an external memory card, so I want to do the same but save file on internal storage for device to make this code run on any device, how can I save file into internal storage?
No.
Your code saves to external memory which is build into the phone.
Your code has nothing to do with a removable micro SD card.

How can I create a filepath for for scanned images in android studio and add to SQL database?

I am creating an app in Android Studio where I use an external fingerprint scanner to collect fingerprint images. The only purpose of the app is to store the scanned image along with custom data for each image in a SQL database in order to use the photos for an image processing project. I am using this library https://github.com/shodgson/uareu that allows me to take a photo with the scanner which works perfectly. My problem is how I can create a filepath for the image and put it in an SQL database along with custom data I have added.I tried to implement this method: https://developer.android.com/training/camera/photobasics#java without any luck.
if you want to store an image to your application and user can't see that you can use internal storage . internal storage is a place in your phone that just your app can access that and user can't.
you can use below pseudo code to access to internal storage and create your image file with appropriate suffix for example .jpg .jpeg and another .
at final you must write your image to output stream of new file.
//this is internal storage file
File file = getFilesDir();
File ImageFile = new File(file.getAbsolutePath()+"/"+newFileName.suffix);
try
{
ImageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(ImageFile);
//write your image to file output stream
} catch (IOException e) {
e.printStackTrace();
}

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");

Make images and folder invisble to Gallery saved on SDcard?

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.

Categories

Resources