Universal Image Loader and BitmapDrawable - android

We have an image in B64, an we want to use that image as loading image in Universal Image Loader while a better image isn't loaded from an url. So we create a bitmap from that url in b64 and convert it into a BitmapDrawable.
The result is shown fine if I do:
imageView.setImageDrawable(bitmapDrawable)
But, on DisplayImageOptions, if I set that bitmapDrawable as image on loading, the image is never shown. I'm doing the following:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(bitmapDrawable)
.showImageOnFail(bitmapDrawable)
.showImageForEmptyUri(bitmapDrawable)
.build()
As you can see, I am setting the bitmap drawable not only as image when loading, but as image when fails too (as we don't want that image to change in case of error while loading the better image from an url). The result of that is that the bitmap drawable is never shown. What are we doing wrong?
UPDATE:
After debugging what was happening I've seen that the problem is not the bitmap drawable, it is supported and working fine. The problem was that I am using a default display options builder (mDisplayImageOptionsDefaultBuilder), than at some point did:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(loadingResource)
.showImageOnFail(errorResource)
.showImageForEmptyUri(errorResource)
.build()
So there's a bug in Universal Image Loader, because now I'm creating a display image options with:
.showImageOnLoading(bitmapDrawable)
Another "solution" is do:
final DisplayImageOptions imageOptions = mDisplayImageOptionsDefaultBuilder.
.showImageOnLoading(0)
.showImageOnLoading(loadingResource)
.showImageOnFail(errorResource)
.showImageForEmptyUri(errorResource)
.build()
But internally it stores that there's a resource stored, so my drawable is not shown but the stored resource instead. Creating a new DisplayImageOptionsBuilder worked for me, but it would be nice that if the showImageOnLoading is set with a drawable, then the old resource was automatically cleared.
Thanks in advance.

UIL only supports the following schemes:
"h t t p://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
Use these schemes.

Universal Image Loader also provide to use the Background functionality.Please check the below coed for it:-
Here Uri is the path of the folder image OR the URL of the image.
imageLoader.loadImage(YOUR_URL, new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
layout.setBackgroundDrawable(new BitmapDrawable(loadedImage));
}
});

Related

Read and load images from SDcard asynchronously - fail

I'd like to load image which is on SDCARD in folder to imageView of my cell in listView. I've tried different solutions but each of them fails. When I load images normally as it is in every base tutorial everything works. I've observed that, my application slows down when it has to load many images. Images are taken from photo camera of device. I'd like to load each of them asynchronously to avoid UI slow reaction. I've tried to use Thread, Asynctask but each of them throws error: "Only the oryginal thread that created a view hierarchy can touch its views". How to load images to avoid speed problems? SDImageLoader is a class which is possible to get from GITHUB. What I've tried is a standard code but is slows:
In a getView method in ListAdapter:
File imageFile = new File(Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png");
String imageFileString = Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png";
// "test.png" is a test file. Each cell will have different name of file to load.
if(imageFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
// final SDImageLoader loader = new SDImageLoader(context);
// new SDImageLoader().load(imageFileString, image);
//UrlImageViewHelper.setUrlDrawable(image, url);
}
else
{
final SDImageLoader loader = new SDImageLoader();
Resources resources = context.getResources();
int resurceId;
resurceId = resources.getIdentifier("bad", "drawable",context.getPackageName());
loader.load(imageFileString, image);
image.setImageResource(resurceId);
}
Have you tried to refresh your project after adding an external library to your project? It doesn't matter with the fragment. You send exact context to the List Adapter - which should be fragment.this.getActivity().

Universal Image Loader Load bitmap from memory

Universal Image Loader provide many ways to load the image.
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
But all these way load image form file, I need a way to load from memory since my images was encrypted and stored in the assets folder, When I display this image, I need the following steps.
decrypt the image into bytes array.
Create bitmap from the bytes.
Load/display the image.
So it's something like this. Is that possible?
Bitmap bitmap = decrypt(encryptedImageFile);
imageLoader.displayImage(bitmap, imageView);
Currently, I am considering to save the bitmap to file and load the file, but this will take more time.
I believe the below is what you are seeking if your images are stored in image folder in assets directory, then you can get the list of images
private List<String> getImage(Context conetx) throws IOException {
AssetManager assetManager =conetx.getAssets();
String[] files = assetManager.list("image");
List<String> it=Arrays.asList(files);
return it;
}
As a note, instead of using assets dir, put the file into /res/raw and you can then access it using the following URI
android.resource://com.your.packagename/" + R.raw.<nameoffile>
I think you need to understand this. You must know, If you have read the source code of universal-image-loader, the order of loading a image into a ImageView after the image's url is provided, is: memory, SDCard(if set), internet. That means after you called, ImageLoader.display(url, imageview);, it will look for the Bitmap from memory first, if it doesn't exist, if will look for the file of the image from SDCard then, if the file exist, if will convert the file into a Bitmap, then load the Bitmap into the ImageView and store it in memory. But if the file doesn't exist, it will download the image file of the url, then store the file into the SDCard and convert the file into a Bitmap and load the Bitmap into memory. Most importantly, I recommend you to read source codes of it, if you are confused with what I post above.
So, it is unnecessary for you to load the Bitmap from memory, ImageLoader will do it for you.
Lets choose own scheme so our URIs will look like "stream://...".
Then implement ImageDownloader. We should catch URIs with our scheme and return image stream.
public class StreamImageDownloader extends BaseImageDownloader {
private static final String SCHEME_STREAM = "stream";
private static final String STREAM_URI_PREFIX = SCHEME_STREAM + "://";
public StreamImageDownloader(Context context) {
super(context);
}
#Override
protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
if (imageUri.startsWith(STREAM_URI_PREFIX)) {
return (InputStream) extra;
} else {
return super.getStreamFromOtherSource(imageUri, extra);
}
}
}
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.imageDownloader(new StreamImageDownloader(getApplicationContext()))
.build();
ImageLoader.getInstance().init(config);
ByteArrayInputStream stream = new ByteArrayInputStream(imgBytes);
String imageId = "stream://" + stream.hashCode();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.extraForDownloader(stream)
.build();
ImageLoader.getInstance().displayImage(imageId, imageView, options);

Save images in external storage from Parse.com

I want to download images from parse database table and store all the images in a external memory folder. Is it possible ? If yes , how ??
Yes of course it is possible. But you need to follow some steps:
-Start downloading the image Url from your Parse object:
String url=yourParseObject.getParseFile("Image").getUrl();
-Now pass that url to this function:
public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
final ImageView imageView = (ImageView) rowView.findViewById(R.id.image);//Your imageView in the layout
AsyncImageLoader async=new AsyncImageLoader();
Drawable cachedImage = async.loadDrawable(
url, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable,
String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
imageView.setImageDrawable(cachedImage);
return cachedImage;
}
-If you want to save that in SD Card instead of ImageView, export that inside of the method (Drawable imageDrawable,String imageUrl) in drawable or bitmap form.
Hope it helps!
you can do that just you need to write code for download image from url and for url you can get using ParseFile.getUrl() and for downloading file you can check this tutorial
http://javatechig.com/android/download-image-using-asynctask-in-android
You can get download image from parse.com in bitmap then store this bitmap as image in your file directory as you want.
Refer this
android-parse-com-image-download-tutorial

How to load thumbnail with Universal Image Loader for Android?

I am using this lib:
https://github.com/nostra13/Android-Universal-Image-Loader
I want to load an image thumbnail into an ImageView using the lib.
What content uri can I pass into:
imageLoader.displayImage(imageUri, imageView); //?
The docs give an example of using a content url:
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
I pretty much want to switch from doing this:
return MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
to using the lib.
As you can see I have the id but I need to construct a url for MICRO_KIND image thumbnails...
I think you can use next URI format: content://media/external/images/thumbnails/13
String uri = "content://media/external/images/thumbnails/" + id;

Populate Android Gallery from image file paths?

I have some images that are saved to a directory on the Android device when the application starts--I would like to be able to display these images in a Gallery, but so far I haven't been able to do it.
I was following the sample Gallery code here, but it uses drawable resource IDs instead of file paths. I found this solution that is similar to what I'm looking for, except it uses ImageView instead of Gallery.
So the code using ImageView would look something like this:
File imgFile = new File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
The above code works, but I'm not sure how to do it with a Gallery. I have been searching for answers and trying different things, but I'm v new to Android development and I feel like I'm in a bit over my head.
Any help is appreciated. Thank you in advance.
Basically I think you just need to put your two examples together.
Use the HelloGallery example as a start, however you want to change the code inside the getView() method of the ImageAdapter to call setImageBitmap() on the ImageView instead of setImageResource().
You will need an array/collection of file paths of images you want to load.
What you need to do is something like this:
public ImageAdapter(Context c, int itemId) {
context = c;
imgArr = GlobalStore.getItem(itemId).getPhotos();
TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
as you can see this is basically a copy from Gallery tutorial. In the constructor imgArr variable is loaded with an array of JPG file names. These were for example read from a database.
Then in the getView function you have something like this...
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
String tmpStr = appContext.getFilesDir() + File.separator + "photos" + File.separator + imgArr.get(position);
Bitmap bitmap = BitmapFactory.decodeFile(tmpStr);
imageView.setImageBitmap(bitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
As you can see getFilesDir() gets your applications data location where it stores files, then let's imagine all photos are in "photos" directory, you build a path and attach a file name from the imgArr array. Since this is called for every photo you just use the passed position variable.
If you don't have an array of photos then maybe the way is to build it by reading the directory where you store photos, load all of the filenames in an array and then do this.
Then you do the rest on the gallery side as in the gallery tutorial.

Categories

Resources