I've started to face an issue with Glide Image Download.
My application is running on Glide version 4.8.0 and minSdkVersion 21, targetSdkVersion 26, and compileSdkVersion 28.
The problem is Glide is unable to download an image from Apple News and causes it to lock and crash application. The URL of the image is https://c.apple.news/AgEXQU9SSmFycEhYUUVlSkxKUE52XzN1M1EAMA
The code to execute the image download is:
Glide.with(context)
.setDefaultRequestOptions(getRequestOptionsForImage(item.ContentPersonStatusUpdate.widthURLImageWidth,context.getResources().getDimensionPixelSize(R.dimen.image_height_external_link),context))
.asBitmap()
.load(https://c.apple.news/AgEXQU9SSmFycEhYUUVlSkxKUE52XzN1M1EAMA)
.into(imageViewContent);
public static RequestOptions getRequestOptionsForImage(int width, int height, Context context){
return new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).override(width,height).placeholder(context.getDrawable(R.drawable.ic_news_thumbnail_loading));
}
Any idea/solution about this issue? This problem only occurs for Apple News Image URLs.
***UPDATE
I've tried to download image with the code below. Getting no responses, even catch never firing.
new DownloadImageTask(imageViewContent).execute(https://c.apple.news/AgEXQU9SSmFycEhYUUVlSkxKUE52XzN1M1EAMA);
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Serkan your code is correct except the request option part.
i try below code and worked fine:
String url="https://c.apple.news/AgEXQU9SSmFycEhYUUVlSkxKUE52XzN1M1EAMA";
RequestOptions requestOptions = new RequestOptions();
requestOptions.error(R.drawable.img_placeholder);
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.load(url)
.into(imgImage);
i try your code too without request option like below:
Glide.with(context)
.asBitmap()
.load(url)
.into(imgImage);
and worked fine too
when i try to use with your request option code its tell me use #RequiresApi(LOLLIPOP) annotation. may you are testing app in below api and the method doesn't work.
Related
I am trying to implement an Android image filter library called GPUImage Located here.
I have tried to use it like below
public static GPUImageView img_bg;
img_bg = (GPUImageView) findViewById(R.id.img_bg);
categoryAdapter1.setOnClickLIstner(new OnClickLIstner() {
#Override
public void onClick(View v, Image image, int pos) {
Glide.with(NameArt.this)
.load(image.getDrawableId())
.centerCrop()
.dontAnimate()
.into(img_bg);
img_bg.setVisibility(View.VISIBLE);
}
});
But I am getting error like below
cannot resolve method 'into' (jp.co.cyberagent.android.gpuimage.GPUImageView)
I am unable to solve it because I am learning android and java yet. Let me know if any expert here can help me for solve the issue. Thanks
Ensure you are using right version of Glide your GPUImageView can deal with. Recent v4 brings API changes that are not backward compatible and since this lib you are using looks bit rusty and does not set properly dependencies, you got the clash. Enforce v3 of Glide, or drop dated library.
try this to load image in your GPUImage
new DownloadImage(img_bg).execute(url);
create a Async Task
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
GPUImageView bmImage;
public DownloadImage(GPUImageView bmImage) {
this.bmImage = (GPUImageView ) bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
i hope that it will work in your case
I want to display image to ImageView from "http://i.imgur.com/4GLKF4Q.jpg" but The image in image URL didn't display to imageview. Can I do this?
AsyncTask
class DownloadImageTask extends AsyncTask<String, Void, byte[]> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected byte[] doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
InputStream is = (InputStream) url.getContent();
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = is.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
return output.toByteArray();
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
protected void onPostExecute(byte[] result) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(result);
Bitmap bm = BitmapFactory.decodeStream(imageStream);
bmImage.setImageBitmap(bm);
}
}
in activity
new DownloadImageTask((ImageView) newView.findViewById(R.id.thumbnail))
.execute("http://i.imgur.com/4GLKF4Q.jpg");
The image in image URL didn't display to imageview.
in Logcat :
09-23 10:26:49.410 10591-10591/com.*** D/skia: --- SkImageDecoder::Factory returned null
Update from comment : changing image server to another one solve the issue. I guess it's a i.imgur.com issue.
You shouldn't use your own image loader task, it will leak somewhere.
Two good library exist :
- Fresco (from Facebook)
- Picasso (from Square up)
There is also UniversalImageLoader (UIL).
I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE). It also reduces OOM(Out Of Memory) issues. When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory.
Now there is an official way to load an ImageView from a URL and that is NOT to use an ImageView.
NetworkImageView—builds on ImageLoader and effectively replaces ImageView for situations where your image is being fetched over the network via URL. NetworkImageView also manages canceling pending requests if the view is detached from the hierarchy.
Images are automatically loaded in a background thread and the view updated on the UI thread. It even supports caching.
I'm trying to load some images from my server to New App, but i don't have any result :
This is my MainActivity code :
import java.io.InputStream;
import static android.net.Uri.parse;
public class MainActivity extends Activity {
private ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadImageTask((ImageView)findViewById(R.id.loadimg)).execute(getString(R.string.link));
private class DownloadImageTask extends AsyncTask<String,Void,Bitmap>{
private final ImageView bmImage;
ImageView bmImg;
public DownloadImageTask (ImageView bmImage){
this.bmImage=bmImage;
}
protected Bitmap doInBackground (String...urls){
String underplay = urls[0];
Bitmap mIcon11 =null;
try {
InputStream in =new java.net.URL(underplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e){
Log.e("Error",e.getMessage());
e.printStackTrace();
}
return mIcon11 ;
}
}
The Result in emulator is not appear any thing just my Button ?
Add the following dependency to your build.gradle file under app:
dependencies {
compile 'com.squareup.picasso:picasso:2.3.2'
}
Then in your code you can simply load a bitmap or image from a server like so:
Picasso.with(this)
.load(IMAGE_URL)
.into(yourImageView);
Where this is the activity context. IMAGE_URL is the url of the image, example: http://yourapi.com/image_1034.png, and yourImageView is the ImageView, ImageButton, or other Custom View where you want to upload the image into.
Doing it this way is considered best practice, and reduces a lot of the boilerplate code you've written. Try building a scalable app writing AsyncTasks for every time you upload a Bitmap.
In your AsyncTask you need to implement:
protected void onPostExecute(Bitmap icon) {
iv.setImageBitmap(icon)
}
Can you try this code?
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.image);
So I am using Picaso to load some images and cache them like so:
ImageView logo = (ImageView)findViewById(R.id.image_logo);
Picasso.with(VenueDetailsActivity.this).load(url).into(logo);
However, I have other images that should not be cached. However, it seems like as soon as Picaso is running in any point in the app, then it starts caching all images no matter if I use Picaso loading on them or not.
How can I not cache certain images using Picasso?
** Does Picasso setups your app that any image loading is cached regardless of using Picasso or not?**
The method I use to download my images are:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
If you don't want Picasso to cache an image you can use .skipMemoryCache() like so:
Picasso.with(VenueDetailsActivity.this).load(url).skipMemoryCache().into(logo);
For more information you can view the documentation here
Just two things, is this a good simple way to grab images? also when i do try it on the android AVD nothing gets displayed on screen as well as in log_cat, no errors or crashes. Here is my code:
public class MainActivity extends Activity {
Bitmap bitmap = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
bitmap = getBitmap("https://twimg0-a.akamaihd.net/profile_images/2275552571/image_normal.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}catch(Exception e){
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}catch(Exception ex) {return null;}
}
}
try : http://code.google.com/p/android-imagedownloader/ .
You can download remote images synchronously, Asynchronously, etc. it works really good
ImageDownloader imageDownloader = new ImageDownloader();
imageDownloader.setMode(ImageDownloader.Mode.CORRECT);
ImageView imageView = (ImageView) rowView.findViewById(R.id.picture);
imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
List<ImageSize> images = this.pictures.get(position).getImages();
imageDownloader.download(images.get(images.size()-3).getSource(), imageView);
It doesn't work because your picture's url starts with HTTPS. Try to use HttpGet. Something like that.