I have an AsyncTask that gets favicon base on a URL.
I am creating a listView, that has a favicon and a URL. at the moment even though I have the AsyncTask my UI waits for the asyncTask to finish before it shows the next activity.
I would like my activity to start with a default image that is stored in drawable, and that the AsyncTask will replace the images after it got each favicon.
Any ideas how to do it?
my AsyncTask:
private class DownloadImageTask extends AsyncTask<URL, Void, Bitmap> {
final AccountListModel model = new AccountListModel(daoSession);
protected Bitmap doInBackground(URL... urls) {
URL urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
mIcon11 = model.getBitmapFromURL(urldisplay);
//try to get image
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
faviconBitmap=result;
}}
the way I am currently creating my list view
for (Site site : model.getSites()) {
try {
String url = site.getDomain()
faviconBitmap= new DownloadImageTask().execute(new URL("http", "www."+ url.trim(),"/favicon.ico")).get();
if (faviconBitmap != null) {
Bitmap scaled = Bitmap.createScaledBitmap(faviconBitmap,32, 32, true);
accountsAndUsersList.add(newAccountListScreen(scaled,site.getName());
}
}
else {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.favicon);
Bitmap scaled = Bitmap.createScaledBitmap(icon, 32, 32, true);
accountsAndUsersList.add(new AccountListScreen(scaled,site.getName());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sitesToSortArray = new AccountListScreen[accountsAndUsersList
.size()];
accountsAndUsersList.toArray(sitesToSortArray);
AccountListAdapter adapter = new AccountListAdapter(this,
R.layout.account_list_row, sitesToSortArray);
listViewAccountList = (ListView) findViewById(R.id.activityAccountList);
listViewAccountList.setAdapter(adapter);
please let me know if you would like to see my adapter as well.
Thank you!
Related
I want to set bitmap for ImageView. I have the URL from Firebase stroage, I use the BitmapFactory to turn to picture, I need to apply it on ImageView, I can do it with AsyncTask but I don't know how to did it with thread or runnable.
AsyncTask cannot use anymore in Android API 30, after I find on Internet, I find a solution that is run with thread and runnable.
These are my code that can work with AsyncTask:
AsyncTask<Void,Void,Bitmap> work = new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... voids) {
URL url = null;
try {
url = new URL("Some URL");
} catch (MalformedURLException e) {
Log.d("Error",e.toString());
}
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
Log.d("Error",e.toString());
}
return bmp;
}
#Override
protected void onPostExecute(Bitmap bmp){
imageMessage.setImageBitmap(bmp);
}
};
work.execute();
I try to do it with thread and runnable but I don't know after I docode, how do I sent and apply it on ImageView. In AsyncTask, I can do that.
I have an image view in my Android app, where I have to set a simple image from url. I tried the below code, but it doesn't set the image from url.
try {
URL url = new URL("https://drive.google.com/...");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream stream = connection.getInputStream();
Bitmap teamBmpImage = BitmapFactory.decodeStream(stream);
teamImgView.setImageBitmap(teamBmpImage);
}
catch (Exception e) {
}
Could someone guide me to achieve this please?
UPDATED CODE: Which gives Nullpointer exception
public class AboutActivity extends ActionBarActivity {
ImageView teamImgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
teamImgView = (ImageView) this.findViewById(R.id.teamImageView);
new DownloadImageTask(teamImgView).execute("http://docs.oracle.com/javase/tutorial/2d/images/examples/strawberry.jpg");
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//pd.show();
}
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;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
//pd.dismiss();
bmImage.setImageBitmap(result);
}
}
}
I guess you are executing your code on the MainThread, which leads to a NetworkOnMainThreadException in android. Try to execute your code asynchronous like in the example below
new AsyncTask<String, Integer, Bitmap>() {
#Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bm) {
ImageView teamImgView = (ImageView) findViewById(R.id.teamImageView);
teamImgView.setImageBitmap(bm);
}
}.execute("https://drive.google.com/uc?....");
You can use Picasso library and here is a detailed tutorial on how to do this.
This is very simple example usage
Picasso.with(activityContext)
.load("https://drive.google.com/uc?....")
.placeholder(R.drawable.image_name)
.into(imageView);
As bojan says you can use Picasso library wich handles many common pitfalls of image loading on Android.
Picasso.with(context).load("http://myurl/myImage.png").into(imageView);
Picasso
Anyway, check out this threat too :)
How to load an ImageView by URL in Android?
Try following this link:
http://www.tutorialsbuzz.com/2014/11/android-volley-url-imageview.html
This will help you to load your image using Volley library which will do all the networking stuff on networking thread and set your image on main UI thread. It has also the LRUCache part which you can skip if you want.
I am trying to display an image in listview from url using JSON parsing. the image url displays correctly in log. when i am trying to download image and display in list getting NullPointerException in bmImage.setImageBitmap(result);
i am using following code can anyone tell me the solution..
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
Bitmap bt_img = null;
try {
FileInputStream in = new FileInputStream(urls[0]);
InputStream in = new java.net.URL(urls[0]).openStream();
bt_img = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return bt_img;
}
protected void onPostExecute(Bitmap result) {
try {
bmImage.setImageBitmap(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you are getting a nullPointerException on the bmImage variable, it means it has not been initialized. As in the code you provide you are not getting its reference, you have to be passing it to the AsyncTask.
What is the code where you pass the bmImage reference, invoking your AsyncTask? The problem seems to be in that code, not in the AsyncTask itself.
1) best way is use Lazy Loding
and
2) second way is try this code,
try {
URL imageURL = new URL(imgUrl);
qrBitmap = BitmapFactory.decodeStream(imageURL.openStream());
image.setImageBitmap(qrBitmap);
} catch (Exception e) {
Log.d("QRDisplay", e.getMessage());
}
You have to use the Universal Image Loader for getting the images from the server.
this link help to u
https://github.com/nostra13/Android-Universal-Image-Loader
Hello I am new with Google Glass and am wondering if I can display an image from the web as my background image.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String address = "http://archiveteam.org/images/1/15/Apple-logo.jpg";
Card myCard = new Card(this);
myCard.setText("Hello, World!");
myCard.setFootnote("First Glassware for Glass");
myCard.setImageLayout(Card.ImageLayout.FULL);
myCard.addImage(new URL(address));
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);
}
I saw in a few threads that myCard.addImage(new URL(address)) was the solution, but I am getting the following error on that line.
The method addImage(Drawable) in the type Card is not applicable for the arguments (URL)
Any help would be appreciated, thanks in advance!
You shouldn't be gathering images on the UI thread. That is a network operation.
Create a separate Method running on a new thread to download the image, then save it as a bitmap. Then save that to the card;
DownloadImageTask(myCard).execute(address);
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
Card imgCard;
public DownloadImageTask(Card imgCard) {
this.imgCard= imgCard;
}
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) {
imgCard.setImage(result);
}
}
I have two functions that access the internet on the APP start. I've tried to use this post as a reference in order to have the popup dialog while my content loads.
The two functions I would use are:
getImage(); //Gets an image from the internet for an imageview
getJson(); //Where the app goes an parses a JSON object for a lazy load listview.
The problem I'm encountering with the post I referenced above is that I try to make the task return null but it causes the app to crash when I do this. So I have this:
private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");
try {
ImageView i = (ImageView) findViewById(R.id.currdoodlepic);
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(imageURL)
.getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
getJson("all");
return "replace this with your data object";
}
I'm not sure what to return.
The type return of the method doInBackground depend of what you need at the post execution :
void postExecute(Object result); // AsyncTask method
Parameter "result" is the return value of doInBackground. So if you need nothing you return NULL.
I found the exact answer here. Here is the code:
ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";
mChart.setTag(URL);
new DownloadImageTask.execute(mChart);
The Task class:
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
...
}