Image downloading is not working in android - android

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();
}
}
}

Related

How to detect the loading of the image from the Internet - progress bar

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();
...
}
}

How can change 4 images load from url in android?

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);

how run asynctask inside asynctask in the same time?

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();
}

How to download an image from Json and store it in gallery?

I need to parse an image from some Json and STORE it on phone gallery . I have no idea how to save pictures on SD card so I need a code for this
thanks .
Try this.. realy help you
public class ImageDownloadTask extends AsyncTask {
CustomProgressBar progressDialog;
Bitmap bitmap;
Drawable drawable;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = CustomProgressBar.show(UpdateActivity.this,
"Loading...", true, true);
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
URL url;
InputStream in;
BufferedInputStream buffIn;
try {
url = new URL(imageUrl);
in = url.openStream();
buffIn = new BufferedInputStream(in);
bitmap = BitmapFactory.decodeStream(buffIn);
drawable = new BitmapDrawable(bitmap);
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (bitmap != null)
imageViewPhoto.setBackgroundDrawable(drawable);
else
imageViewPhoto
.setBackgroundResource(R.drawable.img_default_photo);
}
}

How to set wallpaper of image getting from url in android

From below code I am able to get the image on imageview from server using URL of that image. Now I want to set as wallpaper of that image. Please provide solution.... so that I will be able to set wallpaper without downloading the image in my phone.
onCreate() method
{
image = (ImageView) findViewById(R.id.image);
new DownloadImage().execute(URL);
}
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity1.this);
mProgressDialog.setTitle("Downloading....");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
you should add permission for this
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

Categories

Resources