populating array with images from a Firebase storage folder - java

So I managed to get my Android app working with both Firebase and Glide and I'm able to load a single image from Firebase into an ImageView like this:
imageview1;
imageview1 = (ImageView)findViewById(R.id.ivTest);
String url1 = "https://firebasestorage.googleapis.com/v0/b/zxtrix-e017d.appspot.com/o/fullsize%2Falligator.jpg?alt=media&token=1b85f1c4-4133-41d8-a65f-eg2e263g0403";
Glide.with(getApplicationContext())
.load(url1)
.into(imageview1);
However, I'm currently loading an array of images from my /res/ folder like this, and I want to change it so that it loads from Firebase instead.
Integer[] ApocImages = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16};
I've uploaded all the images in my /res/ folder to my Firebase app storage area.
How can I now load them from Firebase into my array?
Thanks!

You can make an array of URL same as drawable you made.
String[] url = {"https://firebasestorage.googleapis.com/v0/b/zxtrix-e017d.appspot.com/o/fullsize%2Falligator.jpg?alt=media&token=1b85f1c4-4133-41d8-a65f-eg2e263g0403",
"https://firebasestorage.googleapis.com/v0/b/zxtrix-e017d.appspot.com/o/fullsize%2Falligator.jpg?alt=media&token=1b85f1c4-4133-41d8-a65f-eg2e263g0403",
"https://firebasestorage.googleapis.com/v0/b/zxtrix-e017d.appspot.com/o/fullsize%2Falligator.jpg?alt=media&token=1b85f1c4-4133-41d8-a65f-eg2e263g0403"};
After this, set for loop
for (int i=0; i<url.size; i++) {
Glide.with(getApplicationContext())
.load(url.get(i))
.into(imageview1);
}

Related

Using glide to display multiple images

For a scanning ticket app, I want to display full screen image.
The app does sequential operation. You scan your QrCode (first image), the device reach server (second image indicating to wait), then I display a third image if the ticket is validate, else I display the last one.
I'm using glide and the image path is indicated in a second activity, I transmit URI to main activity with a sharedPreferences file.
Here is the issue:
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
where displayUri is
fun displayUri( uri: Uri?, dir:ImageView){//use to display image known by uri, only for full screen
Glide.with(this)
.load(uri)
.error(R.drawable.imagenotfound) // image in case of error
.override(1280, 800) // resizing if user doesn't respect the indicated dim
.centerCrop()//type of resizing
.into(dir)
}
The issue is that only the first image called by displayUri is displayed, the other call show the error image (imagenotfound)
It seem that I've not totally understood the glide extension.
If someone know about this particular issue thanks a lot!!
Here
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri_State_init = Uri.parse(settings.getString("mainUri", "none"))
uri_State_waiting = Uri.parse(settings.getString("waitUri", "none"))
uri_State_validated = Uri.parse(settings.getString("okUri", "none"))
uri_State_refused = Uri.parse(settings.getString("errorUri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
displayUri( uri_State_init, background)
You are just calling the display Image function with the same uri which means if you didn't passed the uri, then uri will be null and you will definitely get errors. What you can do is to send uri like this ,
val settings = getSharedPreferences("PrefFile", 0)
if (S_mod){//priority on S_mod
uri = Uri.parse(settings.getString("main_uri", "none"))
displayUri( uri_State_init, background)
image.setImageResource(R.drawable.empty)
}
Just change the value of main_uri in Shared Preferences whenever you want to change the state. You can also use onSharedPreferencesChanged Listener.

How to retrieve multiple images from Firebase Firestorage and maintaining their order in Android Studio (Java)?

I am trying to retrieve multiple images from Firestorage, each image is associated with a document in Firebase Firestore database (the document ID is equivalent to the image name).
After retrieving all the documents IDs I inserted it into ArrayList<String> POSTS_IDs and I want to fill the ArrayList<File> IMAGE_FILE with the images but the problem is that the images always take a random order and do not maintain the order of the POSTS_IDs.
I mean if the index 3 of POSTS_IDs is equal to "image3", the index 3 of IMAGE_FILE will be image 5 or 6 (it is random some times it comes with the right order).
I want to know if my way is wrong or what is the problem.
Thank you
try {
for (int i = 0; i < POSTS_IDs.size(); i++) {
final File file = File.createTempFile(POSTS_IDs.get(i), ".jpg");
StorageReference reference1 = storageReference.child("images/posts/" + POSTS_IDs.get(i) + ".jpg");
reference1.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(), ""+taskSnapshot.toString(), Toast.LENGTH_SHORT).show();
IMAGE_FILE.add(file); // adding the images to the IMAGE_FILE ArrayList<File>
}
});
}
}catch(Exception e){
Toast.makeText(getContext(), "Error" + e, Toast.LENGTH_SHORT).show();
}
Just Remove the onSuccessListener. here you have IMAGE_FILE.add(file); for the array list so no need of onSuccessListener

How to draw specific drawable resource programmatically?

I have a set of images in my drawable resources. Now I want to be able to change a placeholder image accordingly with image.setImageResource(R.id.name_of_image) programmatically.
My ImageView is defined as ImageView image = findViewById(R.id.placeholder);
How would I do this? I have tried getIdentifier but that didn't work.
The images have the format Name.jpg
EDIT:
I currently have the following construction:
int resID = getResources().getIdentifier(name.toLowerCase(), "drawable", this.getPackageName());
ImageView image = findViewById(R.id.placeholder);
image.setImageResource(resID);
I have changed the image name to lowercase and it is now a png file.
Your images has to be in png format (prefered by android) and must contain only lowercase letters and digits ([a-z0-9_.].
Then you can use
image.setImageResource(R.drawable.name_of_image);
You can get the name of the drawable with this:
String name = context.getResources().getResourceEntryName(R.drawable.name_of_image);
To get the drawable with the string name:
int id = context.getResources().getIdentifier("name_of_image", "drawable", context.getPackageName());
image.setImageResource(id);

Get the name, icon and package name of all the installed applications in android

I want to be able to get the string package name and icon of all the applications installed in my phone.
What I have done:
Browsed and found this solution
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = this.getPackageManager().queryIntentActivities(mainIntent, 0);
for(int counter = 0; counter<=pkgAppsList.size()-1;counter++)
{
Log.e("TAG", pkgAppsList.get(counter).toString());
}
The problem is that it displays package and class name as as a as a mix and I can't get the icon. What I want is to show the user a list of all the applications installed on the phone with the icon. And I also need to be able to get the package name with a List.OnItemClickListener. I assume that using an array of app names and package name which are of same positioning I can do that. But how would I get the app icon and name with package name. Preferably as an Array or ArrayList that I can convert into a listView object. If there is any alternative method even let me know. I am still a beginner in Android.
And please do help.
Please use the below code and directions:
//check this code with some hard work then you will rock
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
ArrayList<AppInfo> res = new ArrayList<AppInfo>();
for(int i=0;i<apps.size();i++) {
PackageInfo p = apps.get(i);
AppInfo newInfo = new AppInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
}
class AppInfo {
String appname = "";
String pname = "";
String versionName = "";
int versionCode = 0;
Drawable icon;
}
For more information try these links:
http://javatechig.com/android/how-to-get-list-of-installed-apps-in-android http://developer.android.com/reference/android/content/pm/PackageManager.html
Using this link from this question. I got the package name. Then using the answer from this question I got the package/app icon. Now as soon as I figure the array adapter to accommodate this. I guess its done then.
My Code:
final PackageManager pm = getPackageManager();
List<applicationinfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
//The log is not required so if you want to and I recommend during release you remove that statement.
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Drawable icon = getPackageManager().getApplicationIcon(packageInfo.packageName)
imageView.setImageDrawable(icon);
Hope this helps all readers as well.
get appliction icon image from package name
Drawable appicon = getPackageManager().getApplicationIcon("com.example.pkgname");
img.setImageDrawable(appicon);

Select multiple images from Photo Gallery on Android using Intents

#See this https://stackoverflow.com/a/15029515/185022
I`m trying to select images from gallery, but i only found the way to select a single image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Is there a way to select multiple images?
Create a custom gallery same like: Android custom image gallery with checkbox in grid to select multiple
First of all you need to use putExtra with your photoPickerIntent
photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE);
Then in your on activity result you should get ClipData from Intent like this
ClipData clipData = data.getClipData();
//Where data is param intent of onActivityForResult
And iterate this clipData to get URI for specific picked image.
for (int i = 0; i < clipData.getItemCount(); i++){
Uri uri = clipData.getItemAt(i).getUri();
}
I hope this helps
Why don't you try ACTION_SEND_MULTIPLE thing. You will receive a set of Uris.
Something like
if (Intent.ACTION_SEND_MULTIPLE.equals(action))
&& Intent.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list =
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable parcel : list) {
Uri uri = (Uri) parcel;
/// do things here.
}
}
Saw this code block on a google-groups post. Just try this out.
Thanks.
I think, you should implement custom gallery for multiple image pick action.
see here in details.

Categories