Currently I can pass the path to an image in the sdcard. Now I want to pass a path to a folder(to the same function BitmapFactory.decodeFile(path)) so that all the images in the folder can be displayed using
image.setImageBitmap(img);
Basically, I want to display all the images in the selected folder as a slideshow, one after the other. Any help / pointers on this?
It's hard to tell exactly what you're asking for here. You've asked a very general question. I think what would be best for you is to use a Gallery widget.
The first step is getting URLs or paths to all the images. I can think of two alternatives.
1) List all files in whatever folder you are looking for and get the ones of the right type. You can use listFiles to get all the files in a folder:
File files[] = Environment.getExternalStorageDirectory().listFiles();
and string split or substring to get the extension
string filename = file[i].getName();
int pos = filename.lastIndexOf(".");
string ext = filename.substring(pos);
2) You can use a content resolver and get the images straight from the Android MediaStore. This solution would be more robust. Search for how to use the MediaStore if you want to use this.
Second step is to use BitmapFactory to get all the bitmaps you need. Yes, you will need to call decodeResource once for each bitmap you get from a file.
Related
EDIT: This question is about using AssetBundle, while that question (List of files in flutter) was about using Directory. They are different because of different classes. ALSO: I removed one section, that can be similar to previous question.
I don't understand how to use AssetBundle for accessing files...
For example, my assets in pubspec.yaml
assets:
- assets/images/
- assets/texts/
AssetBundle has methods: loadString(key, ...) and loadStructuredData(key, ...) - what is a key and how to use this methods?
I need to load data from text files and others files. I know that there is a rootBundle (or DefaultAssetBundle.of(context))... But how to use it to load files?!
Thanks!
Let's assume that you have an image clock.png in assets/images and a UTF-8 encoded text file distances.json in assets/texts.
The key is really just the path to the asset, so you might load the whole file as a String and decode the json like this:
String distancesText = await rootBundle.loadString('assets/texts/distances.json');
Map distances = json.decode(distancesText);
loadString takes care of the UTF-8 decode for you, and also caches the String for faster access next time.
loadStructuredData takes loadString one step further - it loads the String then calls your provided callback to parse the String and returns the result. This time it caches the decoded result - now saving the reading and decoding step the next time.
Map distances2 = await rootBundle
.loadStructuredData('assets/texts/distances.json', (String s) async {
return json.decode(s);
});
So, this is great for text files; what about binary files? You can read the whole asset as a byte array.
ByteData clockData = await rootBundle.load('assets/images/clock.png');
Uint8List clockBytes = clockData.buffer.asUint8List());
Now you can do whatever you need to do with the binary contents of the file. Note that unlike Strings, binary data isn't cached.
Of course, for a PNG, you would most likely not read it as bytes, but instead load it as an Image Widget with AssetImage. (Also asset images should have multiple resolutions for different DPI devices.) See Assets and Images.
I think that earlier you wanted to obtain a complete list of all the assets available. In some ways this makes no sense. You know which assets you provided at build time, so you could keep the list of assets somewhere yourself - in code or in your own manifest. If you really want to enumerate them at runtime, I think you can load an asset called AssetManifest.json, but this seems to be an implementation detail, so possibly subject to change.
I have an idea about the widget that when I click on widget, the text & image will refresh randomly. I have done this with text, but my images are stored on Firebase and I want to take these random images and display them in an ImageView. So how I can do this?
Screenshot of my Firebase Storage:
Screenshot of my App:
As an alternative to #Bernd's answer: You could modify your image names to a standard naming scheme using incremental numbers. You could then retrieve the image URLs dynamically, like so:
Example image names:
image_0.jpg
image_1.jpg
image_2.jpg
image_3.jpg
Some example Java code to generate a random image filepath:
//The amount of images you have stored
int maxImages = 4; //Amount of images
Random random = new Random();
//Randomly generate a filepath to an image
String imageFilePath = "image_" + random.nextInt(maxImages) + ".jpg";
You can then use your generated imageFilePath with FireBase Storage's getDownloadUrl() to retrieve the proper download URL. You can then pass the URL to Glide, Picasso, or another image download library to load it into your ImageView.
Advantages
Only have to use Firebase Storage to achieve your goal
Less overhead on the database, don't have to maintain a list of images there
Disadvantages
You have to control the image names tightly, no custom image names
You have to have a fixed number of images
Could break if you delete an image without changing other image names
Retrieving the URL will throw an exception if the image couldn't be found (e.g. if the random number is out of bounds)
To randomly select an image from Firebase storage, you need to have a list of download url's of the files somewhere. As described in this answer, there is no Api to get a list of these pictures at the moment, so you will have to store the urls of them somewhere.
One possibility for this is to simply add the urls
of the files to Firebase database.
When you want to select a random image, you could go through the database,
and select a random url from this list.
To actually display them in the ImageView, you can use a library like Glide, to make this process easier. The FriendlyPix example App by Firebase, shows how to do this.
I have an activity with a listview and a custom adapter. Retrieving the contact information and the image is not the problem. I can not display an alternative image if there is not one for the contact.
Checking if the URI is NULL does not work for me.
I am retrieving the URI of the android contacts and the resulting URI have the format
content://com.android.contacts/contacts/XXX/photo
where XXX is a number. The resulting URI is only NULL for default contacts added like emergency numbers, so checking for NULL to replace the image with a placeholder image works only on those numbers. For all other contacts that have not set an image i want to check the filesize of the contact image.
File f = new File(uri.getPath());
long size = f.length();
String str = Long.toString(size);
Using the above commands results are always 0, it doesn´t matter if there is a photo or not.
How can i check the filesize correctly? or how can i check if the resulting photo is empty.
Thanks for helping
I encourage you to use Facebook Fresco library. You will be able to provide place holder image aswell as error image for image view
Use any famous image loaders like Universal image loader or picasso lib. This libs will handle every thing, its have some methods for putting error images. And you can directly put your contact URl in this libs.
They will support all kinds of url. you can check here
The idea to check if the file exists led me to this post where it was answered
detect if contact has photo
.getDrawable() == null
gets the correct result.
thanks for all comments
I'm working on an app that includes a large number of videos inside the raw directory in the apk and need to generate thumbnails. I can't seem to get the path right though for the ThumbnailUtils.createVideoThumbnail(path, KIND) also is there a better way than that?
You need to use the AssetManager to get the raw asset path correct http://developer.android.com/reference/android/content/res/AssetManager.html
Consider doing something like:
String [] videos = getAssets.list("raw");
Then iterator through the array and call ThumbnailUtils.createVideoThumbnail on each file that is a video.
I want to make a listview with thumb images in each row and when I stock those images in the database, the querying process became very slow and uneffective, so I am now considering to save a path to the images in assets folder or res folder instead,can anyone tell me how to do that, thanks
An example or tutorial would be much appreciated.
Try use getAssets() method from Context. http://developer.android.com/reference/android/content/Context.html#getAssets()
Hope, it help you!
Why can't you just save the file name?
Then assuming you know the path just load the filename from the database and add it to the path when loading the image. I am doing this to load images from the net.
If you are trying to load a resource you could save the int value that is used to identify the resource.