How can I convert an AsyncTask to a Runnable? - android

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.

Related

Method must be called from main thread,currenty inferred thread is worker

I am trying to change the wallpaper of Android every 15 minutes or something like this. A user can choose the time and I am running a periodic work using Workmanager.
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SomeWorker.class, 15, TimeUnit.MINUTES).build();
WorkManager.getInstance().enqueue(periodicWorkRequest);
This way I am calling my Worker Class. The working class is this
public class SomeWorker extends Worker {
Context context = getApplicationContext();
private String URL;
#NonNull
#Override
public Result doWork() {
new FetchWallpaper().execute();
return Result.SUCCESS;
}
private class FetchWallpaper extends AsyncTask<Void, Void, Void>
{
#Override
protected Void doInBackground(Void... voids) {
try
{
URL = "myurl.com";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
Response responses = null;
try {
responses = client
.newCall(request)
.execute();
String jsonData = responses.body().string();
JSONArray jsonArr = new JSONArray(jsonData);
JSONObject c = jsonArr.getJSONObject(new Random().nextInt(jsonArr.length()));
String imageUrl = c.getString("wallpaper");
Bitmap result= Picasso.with(getApplicationContext())
.load(imageUrl)
.get();
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(result);
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
Date currentTime = Calendar.getInstance().getTime();
}
catch (Exception e)
{
Date currentTime = Calendar.getInstance().getTime();
}
return null;
}
}}
On that Particular line,
new FetchWallpaper().execute();
I am getting the error saying it must call from the main thread. I am new to Android, I don't know if this is the good approach.
Please let me know if there is any better approach to perform such kind of task.
The Worker class already calls doWork on a background thread - you don't need to use AsyncTask at all.
Just move everything from your doInBackground method directly into the Worker's doWork.
You can not update UI from doInBackground method. If you want to do something on UI you must do that on Main UI thread. So write setBitmap code in onPostExecute method as onPostExecute on on Main UI Thread.
To do that set third parameter of AsyncTask as String
AsyncTask<Void, Void, String>
So that return type of doInBackground method will be String
protected String doInBackground(Void... voids)
...
...
return imageUrl;
}
And Your onPostExecute method will be like
#Override
protected void onPostExecute(String imageUrl) {
super.onPostExecute(imageUrl);
Bitmap result= Picasso.with(getApplicationContext())
.load(imageUrl)
.get();
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
new AsyncTask<Void, Bitmap, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
try {
InputStream inputStream;
inputStream = new java.net.URL(url).openStream();
bitmap = BitmapFactory.decodeStream(inputStream);
}catch (Exception e) {
logAppE(TAG, "BITMAP ERROR -> " + e.getMessage());
}
return bitmap
}
#Override
protected void onPostExecute(Bitmap s) {
try {
Glide.with(context).asGif().load(s).into(imgViewGIF);
} catch (Exception e) {
logAppE(TAG, "BITMAP -> " + e.getMessage());
}
}
}.execute();

Setting image from url

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.

Android AsyncTask will not work downloading image

I am hoping someone can help me with a problem I have been having. I am in the process of making my app work on versions greater than 3.0 so I can only perform GUI tasks on the UI Thread. I have got the following code, I am getting no compile errors but it is not working. In the log I get the following error:
I/AndroidRuntime(464): NOTE: attach of thread 'Binder Thread #3' failed
Thank you for your help!
new DownloadImageTask().execute(imgURL); //imgURL is declared as string URL
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
((ImageView) findViewById(R.id.imgCity)).setImageBitmap(bmp);
}
protected Bitmap doInBackground(String... params) {
return loadImage(params[0]);
}
}
public Bitmap loadImage(String poiURLimg) {
try {
URL ulrn = new URL(poiURLimg);
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
} catch (Exception e) {
}
return bmp;
}
The Binder Thread #3 error is not related to the code in your app. There are a number of potential causes usually related to something in eclipse. You can read this post which gives some examples.
As to why the Bitmap wont load - in your onPostExecute you are setting the Bitmap for that ImageView to bmp. Bmp is name of the value that your loadImage method creating the bitmap returns. It is not the name of the Bitmap you pass as an argument into onPosExecute - that is Bitmap result. Change bmp to result and it should work.
A couple things to try. First, make sure you've added the INTERNET permission to your AndroidManifest.xml file. You'll need that to download images from the internet and your app will crash without it (although it doesn't sound like this is your issue)
Try using this for your loadImage() method:
public static Bitmap loadImage(String src)
{
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
Here is an example of a DownloadImageTask that I wrote and use in my apps currently (so I know it works correctly):
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
#Override
protected Bitmap doInBackground(String... params)
{
return loadImage(params[0]);
}
#Override
protected void onPostExecute(Bitmap result)
{
if (isCancelled() || result == null)
return;
((ImageView) findViewById(R.id.image_view)).setImageBitmap(result);
}
}

Change a bitmap image in listView after listView was created

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!

What to return for an Async Task

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) {
...
}

Categories

Resources