How to use Universal image Loader for loading resources locally - android

I want to know how can i use nostra13 / Android-Universal-Image-Loader for displaying Images locally i.e from drawable folder along with the Memorycache. I want to use it with ViewPager.
any help will be greatly appreciated.

To load images from assets and drawables you should take ExtendedImageDownloader from example project (this class is not a part of library yet) and also set it to configuration.
UPD: Loading local resources (from drawable, assets, content provider) works out of the box since UIL v1.8.0.
See README:
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables — ImageView.setImageResource(...) instead of using of ImageLoader.

Whenever More than one image load from resource dynamically (#runtime) than prefer these one:
String imgUri = "drawable://" + getResources().getIdentifier(imgName, "drawable", getActivity().getPackageName());
Here, imgName = Name of image in resource

Related

To display image in ImageView where drawable image ids stored in database column using Picasso

I am using Picasso to set image into ImageView from database where I have stored the drawable image id in the database. It is working perfectly when I store URL(eg https://loremflickr.com/g/320/240/paris)but it's not working for (eg R.drawable.team).
[database image]
//image is the database column containing image ids
Context context = imageText.getContext();
Picasso.with(context)
.load(image)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(50, 50)
.into(imageview);
You to pass the path of the image to set image.Use Glide library to set image it is easy and have more functionality than picasso
Picasso is only a library that makes rendering the image easier through the uri or url path, without picasso you have to create an asyncronous script that is quite complicated. But if you have dynamic data for your drawing path, you can do it with regular looping.
They are now supporting loading Image from URI like the following :
Picasso.get().load(R.drawable.landing_screen).into(imageView1);
Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.get().load(new File(...)).into(imageView3);
From picasso v2+ here is a big modification. The new version is very helpful for manage cash data. It's using Singleton Instance.
By the way, please don't save the drawable ID, just store the drawable name so that later when you call it use the method like the following
private void loadImage(String mImageName, ImageView mImageIcon){
int resID = mContext.getResources().getIdentifier(mImageName , "drawable", mContext.getPackageName());
if(resID!=0) {//The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
mImageIcon.setImageResource(resID);
}
}

Load Image with Universal Image Loader using the Path of Image

I am trying to load an image with UniversalImageLoader. I tried this way:
String path = "/storage/emulated/0/BlackHole/Black Hole/1gatopan0000.png"
ImageLoader.getInstance().displayImage(path, viewHolder.imageView);
But nothing happened. Is it possible to load the image using UniversalImageLoader with the path of the image as a string?
Firstly, you have to add a backslash before any space character in your path.
String path = "/storage/emulated/0/BlackHole/Black\ Hole/1gatopan0000.png"
Then, Picasso is a great library that allows you to load images easily. I personally find it prettier than the UIL. In order to load an image into your ImageView, you simply have to :
Picasso.with(context).load("/your/path/here").into(yourImageView);
Try this:
String path = "/storage/emulated/0/BlackHole/Black\ Hole/1gatopan0000.png"

Picasso load drawable resources from their URI

I have to show a drawable from res into an ImageView. In this app, I'm using Picasso for some reasons.
In this case, I need to load the drawable using its URI and not its id.
To do that, here is my code:
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName);
where drawableName used here are file names rather than their resource ids.
Then
Picasso.with(context).load(uri).into(imageView);
I know for sure that drawable name is correct, but Picasso seems it does not like this uri.
If the images is in your drawable folder then you can just load it.
Picasso.with(context).load(R.drawable.drawableName).into(imageView);
and picasso will load it no need for an Uri.
Found the answer. Unfortunately, Picasso do not allow drawable loading via URI. It is an incoming feature.
This is if you don't want to hardcode the image that you are going
to load...
You can load local image files from your drawable folder lazily if you know the integer value of the image that you want to be loaded.
Then you can just do:
Picasso.with(getContext()).load(imageResourceId)
.error(R.drawable.ic_launcher)
.into(imageView);
Where
imageView
is the view you wish to display the image. For example:
imageView = (ImageView) convertView
.findViewById(R.id.itemImage);
And where
imageResourceId
is the integer value of the drawable. You can retrieve this integer value by:
int productImageId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName());
as well as
productImageName
is the name of the drawable you want to draw (i.e. "ic_launcher")
THIS CAN ALL BE DONE INSIDE FROM THE ADAPTER
From picasso v2+ here is a big modification. The new version is very helpful in order to manage image cache data. It's using Singleton Instance.
GRADLE
implementation 'com.squareup.picasso:picasso:2.71828'
Set drawable image
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
Bonus, get drawable by name:
public static int getDrawableIdFromFileName(Context context, String nameOfDrawable) {
return context.getResources().getIdentifier(nameOfDrawable, "drawable", context.getPackageName());
}
As mentioned in the documentation of Picasso .
they are now supporting loading Image from URI like the following :
load(android.net.Uri uri)
so you have to do something like the following :
Picasso.with(context).load(uri).into(imageView);
just like what you are doing already .
Hopethat helps .

Android - Reference resource drawable as a URL

I'm using an API in my Android app that downloads images from the web and shows a placeholder animation while the images are downloading (RemoteImageView, part of the Ignition package here.
At some points in the app, I need to show images from the local resource drawables and not downloaded from the web.
Is it possible to access the local resource drawables using a URL ?
The way to do it:
use this line "android.resource://[your package]/"+[res id]
Here is an example
Uri myURI = Uri.parse("android.resource://com.example.project/" + R.drawable.myimage);
for convenient you can use it as a method:
public static Uri getUrl(int res){
return Uri.parse("android.resource://com.example.project/" + res);
}
Warning do not use context.getPackageName() as substitute to package name because it might return a null value.
Local image resources do not have urls, they have URIs. So if you have image in drawable, you can parse them from resource id to URI.
Uri uri=Uri.parse("R.drawable.image");
However, if you can also put your images in asset folder of the package and access them using their URL. The URL of the image files would be "file:///android_asset/image.png"
You can use either of the option.

Adding Images to Drawable Folder from a Server

I need to add images into res/drawable folder dynamically as the user select images of his choice from a server...then store the corresponding R.drawable.imageid to Database..so as to load the user chosen images on the next run...Is there no way to do so...?
int[] images = {
R.drawable.m1,R.drawable.m2, R.drawable.m3,
R.drawable.m4,R.drawable.m5, R.drawable.m6,
R.drawable.m7, R.drawable.m8,
R.drawable.m9
};
ImageView iv = (ImageView)findViewById(imageViews[next]);
iv.setImageResource(images[j]);
Here R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8, R.drawable.m9 should be ids of images chosen by user from the server
Quoting Android Engineer RomainGuy
You cannot write to res/drawable.
However you can look for other alternatives like external storage.
Links to consider are
android image save to res/drawable folder
How to convert a Drawable to a Bitmap?
Android: Image save to location
u cannot copy the images in drawable folder.
create folder in data folder and write images there and use bitmap class to map the image.

Categories

Resources