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);
Related
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.
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
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.
This may be more of a Java question than an Android question, but I'm having trouble retrieving a Bitmap created within an AsyncTask to store in another class (an Activity) so that I can recycle it when I'm done using it.
The AsyncTask creates the Bitmap in doInBackground() and sets it as the bitmap for an ImageView in onPostExecute(), the ImageView being passed in through the constructor. But after completion I want the Bitmap to be accessible in the Activity. The Activity has an ArrayList of ImageViews and another of Bitmaps, but since the AsyncTask creates a new Bitmap, I can't find an easy way to get this new object in the ArrayList of Bitmaps in the Activity. Currently I have it working by passing in the ArrayList along with an index into the list to the AsyncTask constructor, and doInBackground just sets that entry in the array to the newly created bitmap.
I don't like this solution though, because I want to be able to use this AsyncTask for different things, perhaps where the Activity doesn't have an ArrayList of Bitmaps. And I can't simply give the AsyncTask constructor a Bitmap because Java passes the reference by value, and setting it to a new Bitmap object wouldn't allow access for the caller.
How can I do this more elegantly?
Here is the relevant code. Lines not pertaining to this question have been omitted for clarity.
public class LoadCachedImageTask extends AsyncTask<String, Void, Void> {
private Context context;
private ImageView image;
private ArrayList<Bitmap> bitmaps;
int index;
public LoadCachedImageTask(Context context, ImageView image, ArrayList<Bitmap> bitmaps, int index) {
this.context = context;
this.image = image;
this.bitmaps = bitmaps;
this.index = index;
}
protected Void doInBackground(String... urls) {
String url = urls[0];
Bitmap bitmap = null;
// Create the bitmap
File imageFile = new File(context.getCacheDir(), "test");
bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
// Set the bitmap to the bitmap list
bitmaps.set(index, bitmap);
return null;
}
protected void onPostExecute(Void arg) {
// Display the image
image.setImageBitmap(bitmaps.get(index));
}
protected void onCancelled() {
if (bitmaps.get(index) != null) {
bitmaps.get(index).recycle();
bitmaps.set(index, null);
}
}
}
And here's a sample Activity that uses it.
public class SampleActivity extends Activity {
private ArrayList<ImageView> images;
private ArrayList<Bitmap> bitmaps;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
images = new ArrayList<ImageView>();
bitmaps = new ArrayList<Bitmap>();
int numImages = 15;
// Create the images and bitmaps
for (int i = 0; i < numImages; i++) {
images.add(new ImageView(this));
bitmaps.add(null);
}
// Load the bitmaps
for (int i = 0; i < numImages; i++) {
new LoadCachedImageTask(this, images.get(i), bitmaps, i).execute("http://random.image.url");
}
}
}
I haven't tested the above code, so it might not work, but I think it gets the point across.
As I understand it, you're trying to load a large number of bitmaps into a large number of ImageViews asynchronously. I would think this can be done with a single AsyncTask class that you use multiple times for each ImageView.
You're AsyncTask should be something like this:
public class LoadCachedImageTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> mImgView;
public LoadCachedImageTask(ImageView image) {
mImageView = new WeakReference<ImageView>(image);
}
protected Void doInBackground(String... urls) {
if(urls == null || urls.length < 1)
return;
// Create the bitmap
final Bitmap bitmap = BitmapFactory.decodeFile(url);
return bitmap;
}
protected void onPostExecute(Bitmap bmp) {
// Display the image
if(bmp != null) {
final ImageView imageView = (ImageView) mImgView.get();
if(imageView != null) // needed in case the weakreference is removed
imageView.setImageBitmap(bmp);
}
}
Then to fill your array of ImageViews with something like this:
for(ImageView imgView : images) {
(new LoadCachedImageTask<String, Void, Bitmap>)(imgView).execute(getBitmapUrl());
}
The for-loop will iterate through each ImageView reference and pass it to a brand new AsyncTask reference. It will then execute the AsyncTask with the given url to whatever bitmap you need. The asynctask will hold on to a reference of the ImageView so long as the ImageView exists. If, for some reason, your ImageView got destroyed, the bitmap will still load then immediately get thrown away.