How to get Uri of an image from url? - android

I want to get uri of an image from url that is stored on server.
I have tried this.
uri = new URI("http://www.google.com/");
and i have also tried this
riuri = Uri.parse( "http://www.facebook.com" );

Do you want to save the image file in your device or what u want exactly.
ImageView.setImageURI(Uri.parse("http://www.facebook.com"));
or u need to save it in your phone .
// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Download Image Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
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();
}
}

Related

Image downloading is not working in 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();
}
}
}

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>

ProgressDialog dismiss is not working

I have a ProgressDialog set up but i ca not dismiss it! it just keep loading the message and wont end. I think I am doing it right, set up the show message in onPreExecute and dismiss it into onPostExecute but some how it's just not working
heres my code:
public class MainActivity extends Activity {
//String[] imgUrls = {getString(R.string.uncc_main_thumb),getString(R.string.football_main_thumb,getString(R.string.ifest_main_thumb),getString(R.string.commencement_main_thumb))};
String[] imgUrls={"http://farm5.staticflickr.com/4113/4843614620_c541de5a5c_m.jpg",
"http://farm8.staticflickr.com/7265/8151547298_85e60e7368_m.jpg",
"http://farm9.staticflickr.com/8054/8414075899_e87a74407b_m.jpg",
"http://farm9.staticflickr.com/8210/8277674769_7d1245dbf1_m.jpg"};
ProgressDialog progressDialog;
ImageView iv1,iv2,iv3,iv4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 =(ImageView) findViewById(R.id.imageView1);
iv2 =(ImageView) findViewById(R.id.imageView2);
iv3 =(ImageView) findViewById(R.id.imageView3);
iv4 =(ImageView) findViewById(R.id.imageView4);
for (String imgUrl : imgUrls) {
new DownLoadImages().execute(imgUrl);
}
}
private class DownLoadImages extends
AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... params) {
String imgUrl = params[0];
Bitmap image = null;
try {
URL url = new URL(imgUrl);
image = BitmapFactory.decodeStream(url.openStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// show the loading message while downloading the image
ProgressDialog.show(MainActivity.this, null, "Loading");
}
#Override
protected void onPostExecute(Bitmap result) {
if (result == null) {
result = ((BitmapDrawable) getResources().getDrawable(
R.drawable.not_found)).getBitmap();
}
//ImageView imageViewToBeAdded = new ImageView(getBaseContext());
// imageViewToBeAdded.setLayoutParams(new LayoutParams(
// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// imageViewToBeAdded.setPadding(5, 5, 5, 5);
iv1.setImageBitmap(result);
progressDialog.dismiss();
}
}
}
You are not creating instance of ProgressDialog.
Replace
ProgressDialog.show(MainActivity.this, null, "Loading");
With
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading");
Use progressDialog instead of ProgressDialog to call show method of ProgressDialog as:
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
issue occurring due to you are calling show method using ProgressDialog but trying to dismiss using progressDialog instance.
In onPreExecute you use the className ProgressDialog.show, which is a static method, in onPostExecute you are actually using your object to stop it.
Use your object to start it in onPreExecute and it will work.
Instantiate your dialog and save the reference:
progressDialog = ProgressDialog.show(MainActivity.this, null, "Loading");

Load two images from url to imageviews

when I tried to load only one image from url, it is working, but when I tried to load two images, it's just loading the last image.
This is what I've tried so far:
ProgressDialog pd;
private ImageView imgView1, imgView2;
private String strURLJohnA = "sample1.jpg";
private String strURLJohnR = "sample2.jpg";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
back = (Button) findViewById (R.id.btBack);
back.setOnClickListener(this);
imgView1 = (ImageView) findViewById(R.id.ivInfo1);
imgView2 = (ImageView) findViewById (R.id.ivInfo2);
// Create an object for subclass of AsyncTask
GetXMLTask task = new GetXMLTask();
// Execute the task
task.execute(new String[] { strURLJohnA, strURLJohnR });
}
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true);
}
// Sets the Bitmap returned by doInBackground
#Override
protected void onPostExecute(Bitmap result) {
imgView1.setImageBitmap(result);
imgView2.setImageBitmap(result);
pd.dismiss();
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
Any ideas how to resolve the issue? I'd greatly appreciate your help. Thanks.
You a list instead as follows:
private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> {
#Override
protected ArrayList<Bitmap> doInBackground(String... urls) {
ArrayList<Bitmap> map = new ArrayList<Bitmap>();
for (String url : urls) {
map.add(downloadImage(url));
}
return map;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true);
}
// Sets the Bitmap returned by doInBackground
#Override
protected void onPostExecute(ArrayList<Bitmap> result) {
imgView1.setImageBitmap(result.get(0));
imgView2.setImageBitmap(result.get(1));
pd.dismiss();
}
...
...
...
You need to return a list of Bitmap objects like I have shown in the code above. Then set the imageView to its respective bitmap.
(String... urls) is Var args. You dont need to pass a string array to it. Use as following code:
task.execute( strURLJohnA, strURLJohnR );
private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> {
#Override
protected ArrayList<Bitmap> doInBackground(String... urls) {
ArrayList<Bitmap> map = new ArrayList<Bitmap>();
map.add(downloadImage(urls[0]));// I used as this for you to understand. You can use for each loop
map.add(downloadImage(urls[1]));
return map;
}
#Override
protected void onPostExecute(ArrayList<Bitmap> result) {
imgView1.setImageBitmap(result.get(0));
imgView2.setImageBitmap(result.get(1));
}
ProgressDialog pd;
private ImageView imgView1, imgView2;
//we can store any number of urls in a string array and load them at a time
private String[] str={"http://www.bogotobogo.com/images/rodin.jpg","http://www.bogotobogo.com/images/smalltiger.gif"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView1=(ImageView)findViewById(R.id.imageView1);
imgView2=(ImageView)findViewById(R.id.imageView2);
// Create an object for subclass of AsyncTask
GetXMLTask task = new GetXMLTask ();
// Execute the task
task.execute(str);
}
private class GetXMLTask extends AsyncTask<String, Void, ArrayList<Bitmap>> {
#Override
protected ArrayList<Bitmap> doInBackground(String... urls) {
ArrayList<Bitmap> map = new ArrayList<Bitmap>();
//enhanced for statement, mainly used for arrays
for (String url : urls) {
map.add(downloadImage(url));
}
return map;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(Info.this, "Please wait", "Downloading content", false, true);
}
// Sets the Bitmap returned by doInBackground
#Override
protected void onPostExecute(ArrayList<Bitmap> result) {
imgView1.setImageBitmap(result.get(0));
imgView2.setImageBitmap(result.get(1));
pd.dismiss();
}

I my using google mapV2 and i m downloading image from google place api and want to display in the popup

i m using all the classes for geting image through Url.
ImageView imV= ((ImageView) view.findViewById(R.id.badge));
// Image url
String image_url = "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png";
final int stub_id = R.drawable.ic_action_search;
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url,stub_id ,imV);
but it is displaying the default image,rather than Url downloaded image.
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.InfoWindowAdapter
i m using infowindowAdapter class of goolge and its says that i cn't change the image when once data & image is loaded in the window,thts why its displaying first default image..... and not changing later
so is there any other way?
thanks in advance
I'm having the same problem.
I've solved it with a asyncTask.
Something like this
private Drawable _infoImageDrawable;
public class MyMapActivity extends MenuActivity
{
.....code code code....
protected void handleMarkerClicked(final Marker marker, final String url) {
new AsyncTask<Void, Void, Void>()
{
#Override
protected void onPreExecute()
{
super.onPreExecute();
_infoImageDrawable = null;
}
#Override
protected Void doInBackground(Void... params)
{
InputStream is;
try {
is = (InputStream) new URL(url).getContent();
_infoImageDrawable = Drawable.createFromStream(is, "");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
marker.showInfoWindow();
}
}.execute();
}
}
Then I just set the drawable in the InfoWindowAdapter to null first(remove old image) and to _imageInfoDrawable second.
It works - but i would like a better solution.

Categories

Resources