How to give a File name automatically when Saving a image - android

When I save the image to the sdcard it automatically saves it with the given file name. If a file with the same name already exist, it is replaced.
I want to store the images with file names in a sequence where if the file Image exists it will be saved with image1 next image2 and so on.
Please advise how to do that, as I am new to programming.

File fileDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] images = fileDir.listFiles(new FilenameFilter(){
public boolean accept(File file, String name){
if(name,toLowerCase().startsWith("image"));
return true;
else return false;
}});
File fileToSave;
if(images.length > 0)
fileToSave = new File(fileDir.getAbsolutePath(), "image" + images.length);
else
fileToSave = new File(fileDir.getAbsolutePath(), "image");
something like that'll do.

you must do two things
1. check is there file with same name
2. change name of the file you want to save
It's simple I think , you can also try to open this file named e.g. "name" , if it opens, change the "name" to "name1" and try it again.
you must keep variable named string e.g. String newName , than
String newName = "name" + 1 , something like this
Regards Hayk Nahapetyan

Cut and paste the image you want to name from the SD card to the internal storage. After renaming, copy the images to SD card or use it as a backup.

Related

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

Open all image from specific folder in Android default gallery

My requirement is to open all images and video's of specific folder.
I have refereed this link, Now I am able to show a image in gallery but I want to show all images from a specific folder. Almost all link I have tried on stack but did not get success.
You can set path in File object initialize at that time.
File folder = new File("/sdcard/Photo/"); in this tutorial default path is /sdcard/photo/ at this place you can set your path then get your files.
I sinc it is not good idea but you may write your own searcher in all files on mobile phone for example
public ArrayList<File> getAllphotos(String path){
ArrayList<File> photoPath = new ArrayList<>();
File yourDir = new File(path);
for (File f : yourDir.listFiles()) {
String mas[] = f.toString().split("\\.");
if(mas[mas.length - 1].equalsIgnoreCase("png") || mas[mas.length - 1].equalsIgnoreCase("jpeg")){//or other formats
//it is picture
photoPath.add(f);
}
}
return photoPath;
}
call this wis iternal and external storage

Should I save external data in directory Android/data or data/?

Which is correct,
String filePath = Environment.getExternalStorageDirectory()
+ "/data/com.packagename";
or
String filePath = Environment.getExternalStorageDirectory()
+ "/Android/data/com.packagename";
if I want to store data in external storage? I see many apps are using the second option, but some use the first path.
You should rely on the API to figure out the directory for you:
File externalDir = Context.getExternalFilesDir(null);
Context.getExternalFilesDir will return your 2nd path. Programs that return the 1st path probably hardcoded the path and got it wrong as a result.

Cannot open image file stored in sdcard

In my application I save the contents of a tableLayout as an image in a folder. To allow user to open a file from the saved images I've created a text file that contains the names of these files. These file names will be loaded in an array (files) later. User clicks on "open" to see a list of file names and selects the one he wants to open. I'm using the following code to open a file.
final String imageInSD = extStorageDirectory+"/myFolder/"+files[which];
//where 'files' is an array of strings and contains the names of files.
//and 'which' is the index of the selected element in the list
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
when I try it nothing happens so I tried the following
final String imageInSD = extStorageDirectory+"/myFolder/myFile.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
And it shows the image named myFile.
I've already checked if I'm getting the correct file name and path and it seems to be correct. (when i click on myFile.PNG in the list and show the path I get "/mnt/sdcard/myFolder/myFile.PNG").
Why doesn't it work when I use the first code?
String concatenation isn't a good way to combine paths. It is better to use the File constructor :
File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());

Android, how do can I get a list of all files in a folder?

I need the name (String) of all files in res/raw/
I tried:
File f = new File("/");
String[] someFiles = f.list();
It looks like the root directory is the root of the android emulator...and not my computers root directory. That makes enough sense, but doesn't really help me find out where the raw folder exists.
Update: Thanks for all the great replies. It appears that some of these are working, but only getting me half way. Perhaps a more detailed description will aid
I want to get all the mp3 files in the raw folder so I can get all the names, then add them to a URI to play a random MP3 in the following way...
String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis";
Uri uri = Uri.parse(uriStr);
singletonMediaPlayer = MediaPlayer.create(c, uri);
When I put "dennis.mp3" into the assets folder, it does show up as expected, however, using the above code, I can't access that MP3 anymore, unless there is something along the line of:
String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis";
Uri uri = Uri.parse(uriStr);
To list all the names of your raw assets, which are basically the filenames with the extensions stripped off, you can do this:
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Since the actual files aren't just sitting on the filesystem once they're on the phone, the name is irrelevant, and you'll need to refer to them by the integer assigned to that resource name. In the above example, you could get this integer thus:
int resourceID=fields[count].getInt(fields[count]);
This is the same int which you'd get by referring to R.raw.whateveryounamedtheresource
This code will retrieve all the files from 'New Foder' of sdCard.
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "New Folder");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
Log.i("file names", name);
}
}
and also make sure to add android sd card write permission in your manifest.xml file
I need the name (String) of all files in res/raw/
There are no files in res/raw/ on the device. Those are resources. There is no good way to iterate over resources, other than by using reflection to iterate over the static data members of the R.raw class to get the various ID names and values.
but doesn't really help me find out where the raw folder exists.
As a folder, it only exists on your development machine. It is not a folder on the device.
You can use AssetManager:
As far as I can remember, you will have list with (just try different paths):
final String[] allFilesInPackage = getContext().getResources().getAssets().list("");
Look at http://developer.android.com/reference/android/content/res/Resources.html
You can generally acquire the Resources instance associated with your application with getResources().
Resources class provides access to http://developer.android.com/reference/android/content/res/AssetManager.html (see to getAssets() method).
And finally obtain access to your packaged (apk) files with AssetManager.list() method.
Enjoy!
From a Context, you can call something like this:
getResources().getAssets().list("thePath");
link to documentation

Categories

Resources