I have four buttons....
Each button is a picture....
I want to change the pictures when I press the next button,,,
How can do this? because my project it is about change 10 photos loading from url>>>>>thanks for help :)
public void onClick(View arg0) {
// TODO Auto-generated method stub
new LoadImage().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
new LoadImag().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
new LoadIm().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
new LoadIma().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
}
});
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
private class LoadImag extends AsyncTask<String, String, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
b1.setImageBitmap(image);
pDialog.dismiss();
}
}
}
private class LoadIm extends AsyncTask<String, String, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
b2.setImageBitmap(image);
pDialog.dismiss();
}
}
}
private class LoadIma extends AsyncTask<String, String, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
b3.setImageBitmap(image);
pDialog.dismiss();
}
}
}
}
You can clean your code using Picasso.
You just need this line:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Related
I have a class:
public class DownloadImageFromInternet extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
public DownloadImageFromInternet(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String imageURL = urls[0];
Bitmap bimage = null;
try {
InputStream in = new java.net.URL(imageURL).openStream();
bimage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error Message", e.getMessage());
e.printStackTrace();
}
return bimage;
}
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
I want to create a progress bar that will detect the loading of the image I download from the internet.
How to do it?
This is how it's done:
public class DownloadImageFromInternet extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private ProgressDialog pd;
public DownloadImageFromInternet(ImageView imageView) {
this.imageView = imageView;
}
protected void onPreExecute() {
pd = new ProgressDialog(); //Create dialog
pd.show(); // show dialog
}
protected Bitmap doInBackground(String... urls) {
...
..
return bimage;
}
protected void onPostExecute(Bitmap result) {
pd.dismiss();
...
}
}
i have an web-based application that must retrive text in external database and image in server after one text retrived ,using following code for retrive text:
class BackgroundTaskOne extends AsyncTask<Void, Void, String>
{
String json_url;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
json_url="http://..some url../somefile.php";
}
#Override
protected String doInBackground(Void... voids) {
// TODO Auto-generated method stub
String error="";
try {
URL url=new URL(json_url);
HttpURLConnection httpUrlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpUrlConnection.getInputStream();
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while((JSON_STRINO=bufferReader.readLine())!=null){
stringBuilder.append(JSON_STRINO+"\n");
//add new textView
}
bufferReader.close();
inputStream.close();
httpUrlConnection.disconnect();
json_string=stringBuilder.toString().trim();
return stringBuilder.toString().trim();
// return "one row of data inserted..";
} catch (MalformedURLException e) {
error=e.getMessage()+" first";
} catch (IOException e) {
// TODO Auto-generated catch block
error=e.getMessage()+" sec";
}
return error;
}
#Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
json_string=result;
l=parse();
int i=0;
for(final String a[]:l){
TextView t=new TextView(MainActivity.this);
final String path=a[2]+".jpg";
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setText(a[0]+" : "+a[1]+" i="+i);
t.setId(i);
Reklam.addView(t);
new LoadImage().execute(path);
i++;
}}}
i want to load image while loading first text and second image after second text in sequence but it loads all text then load all images , these folowing code for loading image:
private class LoadImage extends AsyncTask<String, String, Bitmap> {
Bitmap bitmap;
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
ImageView img=new ImageView(MainActivity.this);
img.setImageBitmap(image);
img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Reklam.addView(img);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
if you want to run multiple thread parallely :
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}
I am trying to download the image from the URL http://dbh_cache.s3.amazonaws.com/19445/34173cb38f07f89ddbebc2ac9128303f-33b64a2ed0f1ff4750f183b4f2a161b8.png
It seem the domain of the URL contains underscore which results in image download failure. Please let me know if i am correct
public class LoadImage extends AsyncTask<String, String, Bitmap> {
ImageView img;
Bitmap bitmap;
Context con;
ProgressDialog pDialog;
public LoadImage(Context updateProfileActivity,
ImageView profileImageView) {
// TODO Auto-generated constructor stub
con = updateProfileActivity;
img = profileImageView;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(con);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
pDialog.dismiss();
}else{
pDialog.dismiss();
// Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
im fetching an image from internet using the below code using an Async task,But the bitmp returns from the function is always null.
private Bitmap asyncTaskFetchImage(final String imgeurl) {
// TODO Auto-generated method stub
new AsyncTask<Object, Object, Object>() {
#Override
protected void onPreExecute() {
progress_Dialog = ProgressDialog.show(this, "", "Loading");
}
#Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try
{
toSendBg=LoadImageFromURL(imgeurl);
System.gc();
return 0;
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
#Override
protected void onPostExecute(Object result) {
if (progress_Dialog != null) {
progress_Dialog.dismiss();
}
}
}.execute();
return toSendBg;
}
Is this the exact way to return value from an Asyntask?
Try below code to download image from web using AsyncTask and display in imageview.
public class MainActivity extends Activity {
ImageView mImgView1;
static Bitmap bm;
ProgressDialog pd;
String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
BitmapFactory.Options bmOptions;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgView1 = (ImageView) findViewById(R.id.mImgView1);
pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
"Carregando...");
new ImageDownload().execute("");
}
public class ImageDownload extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
loadBitmap(imageUrl, bmOptions);
return imageUrl;
}
protected void onPostExecute(String imageUrl) {
pd.dismiss();
if (!imageUrl.equals("")) {
mImgView1.setImageBitmap(bm);
} else {
Toast.makeText(MainActivity.this,
"Não foi possível obter resultados", Toast.LENGTH_LONG)
.show();
}
}
}
public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bm = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bm;
}
private static InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
}
private Bitmap asyncTaskFetchImage(final String imgeurl) {
Bitmap bmp=null;
new AsyncTask<Object, Object, Object>() {
...
and in your doInBackground method change return to
return toSendBg;
and
#Override
protected void onPostExecute(Object result) {
if (progress_Dialog != null) {
progress_Dialog.dismiss();
bmp=(Bitmap)result;
}
}
}.execute();
return bmp;
Try this..,.
I have parsed several items of weather data from an online xml file. One of the nodes is a URL of an image I want to display. I have managed to parse it and save it as a String variable and it displays in the app as a String. How do I get it to display the image instead of text?
Thanks in advance!
First, you need to download the image and store it in a Bitmap object.
Then, display it with an ImageView.
This answer describes how you can do it in some detail.
public class MainActivity extends Activity {
ProgressDialog pd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("Your URL");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
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) {
pd.dismiss();
bmImage.setImageBitmap(result);
}
}
}