I'm trying to load an image from the following url:
https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7
In Chrome/IE the image will be displayed without problems. When I try to load the image on Android I am not able to store the image as an PNG file. Everytime the image will be saved as a textfile which contains the image as base64 String.
Also I tried the following code but the decodedByte is null:
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Does anyone have an idea how I can show my image from the url as an bitmap or which I prefer to store the image on the device as an png?
Use Universal Image Loder to upload image from Url
Download universal image loder and add to your lib folder and
Implement the below code where you want to load image
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.noimage)
.showImageOnFail(R.drawable.icon3)
.showImageOnLoading(R.drawable.loading).build();
imageLoader.displayImage(url, imageview, options);
For handling images, Use Picasso , Glide , Universal image loader, Volley or Fresco
Eg, in picasso, you will be able to load images, store them in memory cache and handle cancellations in listviews/recyclerviews etc...
Picasso.with(context).load("https://fangkarte.de:8080/fangfisch/file/files/592036af55dfffca0155e01fe10002f7").placeholder("someimagetillyourimagedownloads).into(R.id.yourimageview);
Picasso: https://github.com/square/picasso
Glide: https://github.com/bumptech/glide
Fresco: https://github.com/facebook/fresco ( I use this after extensively testing all 3 except volley )
UIL: https://github.com/nostra13/Android-Universal-Image-Loader
Similar things can be done using all the above libraries.
I highly recommend not trying to handle images on your own. It can turn into quite a mess if you are a novice android dev.
The image you download is much to big ((860.000 bytes)) to make a Bitmap out of it. So BitmapFactory will return null.
Scale image down before use.
Related
I am developing A App which requires loading multiple images from their urls. I save all the images in firebase storage. I am currently using a compressor to reduce the size.
newImageFile=new File(uri1.getPath());
try {
compressedImageFile = new Compressor(ProductAdd.this)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumbData1 = baos.toByteArray();
and I using Glide to load the image from the url
Glide.with(context).load(imgUrl).into(holder.cutImg);
I wanted to know if there is any way I could improve to load images much faster. Maybe store them in a specific format.
You can load a thumbnail of the original image into the ImageView before the original image loads.
Glide.with(fragment)
.load(url)
.thumbnail(0.05f)
.into(imageView);
This will load the 5% quality of the original image into the view and when the complete image loads it will be replaced.
And if you're having a separate URL for thumbnail then
Glide.with(fragment)
.load(url)
.thumbnail(
Glide.with(fragment)
.load(thumbnailUrl))
.into(imageView);
Now when image loads it will be immediately loaded into the view, here you can use the transition for a smooth effect.
Glide.with(fragment)
.load(url)
.thumbnail(0.05f)
.transition(DrawableTransitionOptions.withCrossFade())
.into(view);
You can checkout my project in which I'm also first compressing the Image and storing a thumnail image to Firebase Storage and then loading it.
You can also try different DiskCacheStrategies but I would suggest to go with the default one, it works just fine!
Here are various DiskCacheStrategies:
.diskCacheStrategy(DiskCacheStrategy.ALL)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
You can use the disc strategy which will be used to store the image on the disc. So it may take time for loading at first but later it does not take time at all.
For Glide 4.x
Glide
.with(context)
.load(imgUrl)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(holder.cutImg);
For Glide 3.x
Glide
.with(context)
.load(imgUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.cutImg);
Please note: This will not refresh the image if you just update the image without updating URL. SO if you want to see the updated image you must update URL and Glide will automatically download an image for you.
if you are using firebase storage for host your images : Using FirebaseUI you can quickly and easily download, cache, and display images from Storage with Glide. source
Glide.with(context).load(model.imageUrl).thumbnail(0.05f).into(holder.imageView)
I need to download images from server and store as thumbnails directly.
and for display images also i need to use images from .thumbnails folder directly. i am not getting how to create and save images as .thumbnail . and how to use images from that .thumbnail file.i searched online but everywhere only this below code present.
Bitmap thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);
any help?
Please check below code which helps you.
Follow below steps:
Calculate the maximum possible inSampleSize that still yields an image larger than your target.
Load the image using BitmapFactory.decodeFile(file, options), passing inSampleSize as an option.
Resize to the desired dimensions using Bitmap.createScaledBitmap().
Now you have your bitmap ready and you can save it any where using the following code
Bitmap thumbnail;
File thumbnailFile = ...;
FileOutputStream fos = new FileOutputStream(thumbnailFile);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
hope it helps you and save your time.
Or use Glide library which store image as a full image and 1 thmbnail image which is power caching library which loads image quickly and once download image after load image from cache not from network.
Below is a Glide link please read it advantages and features and after used into your applications.
https://github.com/bumptech/glide
I'm using Sqlite to save images and this is the code I use to bring they back to a simple imageView:
byte[] bookImage = books.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(bookImage, 0, bookImage.length);
holder.imageView.setImageBitmap(bitmap);
Now I'm trying to use the Picasso library to prevent a 'Out Of Memory' crash.
This is the code:
Picasso.with(holder.imageView.getContext()).load(bitmap).into(holder.imageView);
Unfortunately i cannot use load(bitmap). How can i use the Picasso library correctly for this?
You could use Glide for this:
Glide.with(holder.imageView.getContext()).load(bookImage).asBitmap().into(holder.imageView);
I want to download images with universal image loader but I do not want it to decode the images and create bitmap from it. I just want to save the original image in a directory. how can I disabling it to decode?
Can I use other libraries? for example picasso but I think it can not do that, am I right?
I think the answer would be:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.NONE)
.build();
because of:
public enum ImageScaleType {
/** Image won't be scaled */
NONE,
from
https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java#L24
For the below - I don not have the URL of the image! I cannot use the URL.
If I am downloading images from the internet into a Bitmap array like this:
Bitmap image = BitmapFactory.decodeStream(file.getReadStream());
//add to the Bitmap Array
images.add(image);
Can this be sent to Picasso (or any other Image loader like UIL?)
I know I can display the images in my Adapter but I am getting out of memory issues. And I thought Picasso could take care of this and the caching etc.
This will not work as it will not accept a Bitmap source. Is there any other library to do this?
Picasso.with(context).load(images.get(position)).resize(60, 60).error(R.drawable.error_icon).into(holder.iconImage);