Picking an image for Wallpaper - android

I'm writing a live wallpaper and need some help. My wallpaper will create an effect over top of another image or existing wallpaper (not another live wallpaper) that the user chooses in the "Settings...".
My problem is this: I can't find a way to list the static wallpapers or images on the phone. I've seen some examples of getting the camera images, but not the wallpapers.
Any help would be appreciated.

If this helps, here's a FileFilter I wrote that will return a list of folders that contain images. You simply take a File representing a directory (I use it for Environment.getExternalStorageDirectory()) use .listFiles(filterForImageFolders) and it will return a File[] with the directories that contain images. You can then use this list to populate your list of images in your settings:
FileFilter filterForImageFolders = new FileFilter()
{
public boolean accept(File folder)
{
try
{
//Checking only directories, since we are checking for files within
//a directory
if(folder.isDirectory())
{
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) return false;
//For each file in the directory...
for (File file : listOfFiles)
{
//Check if the extension is one of the supported filetypes
for (String ext : imageExtensions)
{
if (file.getName().endsWith("." + ext)) return true;
}
}
}
return false;
}
catch (SecurityException e)
{
Log.v("debug", "Access Denied");
return false;
}
}
};
(ImageExtensions is a String[] containing "png", "bmp", "jpg", "jpeg")

Related

File.deleteRecursively() - how to delete the folder's content and not the folder itself

I have a folder that contains picture files.
I use File.deleteRecursively() to delete the files, and it works perfectly,
but I must delete the content, not the 'Pictures' folder itself. How can that be done?
This should work
private static void deleteDirContent(String path){
File f = new File(path);
File[] allFiles = f.listFiles();
if(allFiles == null)
return;
for (File f2 :
allFiles) {
if(f2.isFile()) {
if (f2.delete()) {
// this means file is deleted, everything is good
} else {
// was unable to delete the file, handle accordingly.
}
}
else{
f2.deleteRecursively();
}
}
}
Make sure you have the right permissions of course.
For some reason I don't have deleteRecursively function, if it's Kotlin implementation then for Java you can add this function:
private static void deleteDirContentWithDir(String path){
File f = new File(path);
File[] allFiles = f.listFiles();
if(allFiles == null)
return;
for (File f2 :
allFiles) {
if(f2.isFile()) {
if (f2.delete()) {
// this means file is deleted, everything is good
} else {
// was unable to delete the file, handle accordingly.
}
}
else{
deleteDirContentWithDir(f2.getAbsolutePath());
}
}
f.delete();
}
This will delete the dir content. In general, you can combine them to a single function:
private static void deleteDirContent(String path, boolean delWithDir){
File f = new File(path);
File[] allFiles = f.listFiles();
if(allFiles == null)
return;
for (File f2 :
allFiles) {
if(f2.isFile()) {
if (f2.delete()) {
// this means file is deleted, everything is good
} else {
// was unable to delete the file, handle accordingly.
}
}
else{
deleteDirContent(f2.getAbsolutePath(), true);
}
}
if(delWithDir)
f.delete();
}
This basically says, delete a directory content, including directories that might have content and lets you decide if you want to delete the directory it self using the boolean parameter
If you can use Kotlin version 1.8 or later, there is a new extension function on java.nio.files.Path which is preferable to File.deleteRecursively because it gives you more information (through exceptions) when a file cannot be deleted. It is currently "experimental":
import kotlin.io.path.*
#OptIn(ExperimentalPathApi::class)
fun deleteDirectoryContents(dir: String) {
Path(dir).forEachDirectoryEntry { it.deleteRecursively() }
}

Android: how to populate GridView from read SD Card Folder images?

I have a part of code which search my folders, which have images. Can somebody show me how to add them to GridView and how after using OnItemClickListener to show images in them?
public static List<File> findImageDirectories(File parentDirectory){
List<File> directories = new ArrayList<File>();
if (parentDirectory.listFiles() != null){
for (File file: parentDirectory.listFiles()){
// If the parentDirectory contains an image file
if (isImageFile(file)){
directories.add(parentDirectory);
break;
}
// If it contains a subfolder, check the subfolder as well
else if (file.isDirectory()){
findImageDirectories(file);
}
}
}
return directories;
private static boolean isImageFile(File f) {
String name = f.getName();
if (name.endsWith(".jpg") || name.endsWith(".png"))
// Add other formats as desired
{
return true;
}
return false;
}

how to browse only text files from my application

i have built an application,where i am supposed to browse only for txt files in the device and read it.but i am not getting a valid solution to it.Like for example if we take whatsapp,there you can browse different files under different headings like under videos you will only browse videos and under gallery you will only browse images.but whats in case for text files only?is there any way?Please suggest.
try this.
//for example your folder present in sdcard/cts.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cts");
for (File wavfile : f.listFiles(new FileExtensionFilter()))
{
String str = wavfile.getName().toString();
}
//create a class and the filter based on your need file extension
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".txt") || name.endsWith(".TXT") );
}
}
thank you.
I would put your directories your searching into arrays... Loop the arrays and look for the file extensions after the " . " if its not a text extension remove it. Then display the new array to user like the photo gallery works or however else you would want to represent them.
Note- If you need access to these later you may want to be saving the path if the user say...needs to click and open them...
you may be able to use something to this effect for searching the SD Card
File file[] = Environment.getExternalStorageDirectory().listFiles();
file[i].getAbsolutePath();
Then search it for the extensions you want...
This will give you a file manager as you like. Just needs a bit of work around. But definitely helps
//fill method is to get the dir
private void fill(File f)
{
File[] dirs = f.listFiles();
this.setTitle("Current Dir: " + f.getName());
List<Option> dir = new ArrayList<Option>();
List<Option> fls = new ArrayList<Option>();
try
{
for (File ff : dirs)
{
if (ff.isDirectory())
{
dir.add(new Option(ff.getName(), "Folder", ff.getAbsolutePath()));
}
else
{
String filetype[] = objOption.getName().split("\\.");
if (filetype[(filetype.length) - 1].equals("txt")) {
fls.add(new Option(ff.getName(), "File Size: "+ ff.length()+" bytes", ff.getAbsolutePath()));
}
}
}
}
catch (Exception e)
{}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
{
dir.add(0, new Option("..", "Parent Directory", f.getParent()));
}
adapter = new FileArrayAdapter(FileChooser.this, R.layout.dialog_file_view,dir);
lvFileList.setAdapter(adapter);
}

android emulator sd card

I want to read all the music files from the sd card but I tried to put the file in several places on the SD card and the program still doesn't find the music on the card. Anyone any suggestions? Where exactly to put the files?
I was just doing that last night :).
In android, the sdcard is mounted on '/sdcard'. I don't have the code here, but it was something like this.
public List<String> getMp3Files() {
File sdcard = new File("/sdcard");
return getMp3Files(sdcard);
}
private List<String> getMp3Files(File directory) {
List<String> mp3files = new ArrayList<String>();
File[] files = directory.listFiles();
if(files == null) {
return Collections.EMPTY_LIST;
}
for( File file : files) {
if( file.isFile() && file.getName().toLowerCase().endsWith(".mp3")) {
mp3files.add(file.getAbsolutePath());
} else if ( file.isDirectory()) {
mp3files.addAll(getMp3Files(file));
}
}
return mp3files;
}
it's not optimized AT ALL for mobiles, but it works. And be careful that if you run this code starting on the root folder, it will end up in a infinite loop of folders! (you can always try :D).
Maybe this could help you
http://blog.jayway.com/2009/04/22/working-with-sd-cards-in-the-android-emulator/

Images from folder on sd card

I have an application that needs to read images from a folder created by the application on the sdcard(sdcard/"foldername"/"filename.jpg". I have no idea what the names of the files are because the user specifies the names of the files. I need to read the images from the folder and make something like the default image viewer. Im thinking read them into a grid view first but 1) cant figure out how to dynamically read them from a folder 2) how would I implement the image options like the default viewer? If there was a way to open the default viewer on a certain folder that would help.
any input would be amazing been working on it for a while.
Thanks
Here's how you can get a list of folders off of the memory card:
String state = Environment.getExternalStorageState();
if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
String homeDir = Environment.getExternalStorageDirectory();
File file = new File(homeDir);
File[] directories = file.listFiles();
}
else
{
Log.v("Error", "External Storage Unaccessible: " + state);
}
This code is from the top of my head, so some syntax may be off a bit, but the general idea should work. You can use something like this to filter down the folders to only folders that contain images:
FileFilter filterForImageFolders = new FileFilter()
{
public boolean accept(File folder)
{
try
{
//Checking only directories, since we are checking for files within
//a directory
if(folder.isDirectory())
{
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) return false;
//For each file in the directory...
for (File file : listOfFiles)
{
//Check if the extension is one of the supported filetypes
//imageExtensions is a String[] containing image filetypes (e.g. "png")
for (String ext : imageExtensions)
{
if (file.getName().endsWith("." + ext)) return true;
}
}
}
return false;
}
catch (SecurityException e)
{
Log.v("debug", "Access Denied");
return false;
}
}
};
Then, change the first example to:
File[] directories = file.listFiles(filterForImageFolders);
That should return only directories that contain images. Hopefully this helps some!

Categories

Resources