I want to open a url of an image from another url of an image using intent.I am new to android so this code of mine not working.
url_1 = new URL("http://garooh.905pm.com"+com.org.constant.Helper.Gadd_list.get(0).thisEvent.getImage_four_thumb());
url_2 = new URL("http://garooh.905pm.com"+ com.org.constant.Helper.Gadd_list.get(0).thisEvent.getImage_four_thumb());
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(url_1, url_2);
startActivity(intent);
Am i right in thinking you have an imagebutton and when you click it you want to go to 1 of those URLs?
Try adding this to your imageButton.
imageButton.setOnClickListener(new locatorButtonClickListener());
private class imageButtonListener implements OnClickListener
{
#Override
public void onClick(View button) {
new DisplayImageFromUrl((ImageButton) findViewById(R.id.imageButtonEnd), this).execute(//Enter your link here);
}
UPDATE
Try using this Async task
//Display image to bitmap using URL
public class DisplayImageFromUrl extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
Context context1;
ProgressDialog pd;
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(context1);
pd.setMessage("Loading Images...");
pd.show();
}
public DisplayImageFromUrl(ImageView bmImage, Context context) {
this.bmImage = bmImage;
this.context1 = context;
}
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) {
bmImage.setImageBitmap(result);
pd.dismiss();
}
}
and you call it like this in your activity.
new DisplayImageFromUrl((ImageButton) findViewById(R.id.imageButtonEnd), this).execute(//Enter your link here);
Related
Hi guys i have an problem,
I have a list and I have to view it in a list using the reycleview but when I go to the image via cericare phat and to do this use picasso
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
this.viewHolder=viewHolder;
if(scheduler.size()>=1){
viewHolder.time.setText(scheduler.get(i).getTime());
viewHolder.data.setText(scheduler.get(i).getData());
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(i);
}
});
String phat=scheduler.get(i).getImageString();
//result paht: content://com.android.providers.media.documents/document/image%3A445
Picasso.with(c).load(phat).into(viewHolder.img);//does not display the image
viewHolder.img.setOnClickListener(new MyClikListener(i));
viewHolder.time.setOnClickListener(new MyClikListener(i));
viewHolder.data.setOnClickListener(new MyClikListener(i));
viewHolder.card_view_reminder.setOnClickListener(new MyClikListener(i));
}
}
I also tried converting the pat in a URI but nothing.
Making debugging tells me onBitmapFailed
Use the below code:
Picasso.with(c).load(new File(phat)).into(viewHolder.imageView);
Hope this helps.
Try this
new DownloadImage(yourImageView).execute(URL);
create AsyncTask for download image from background process
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
CircleImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = (CircleImageView) bmImage;
}
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.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
It will work for all..
Well I am downloading a image from url with AsyncTask,
I want to show this image in dialog, right after image downloaded.
I m trying with this code: It ıs not waiting until finish, this code is returning RUNNING.
if(dt.getStatus() == AsyncTask.Status.FINISHED) {
dialog.show();
}
You need to set an event listener. Just setting an if-then statement means it will be executed the moment it finds the code. A event listener will wait until the event happens (In this case, Status.FINISHED) and then execute the code inside the listener.
With Ken Wolf helps..
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
private Context mcontec;
public DownloadImageTask(ImageView bmImage, Context mContextr) {
this.bmImage = bmImage;
mcontec=mContextr;
}
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) {
dialog(result, mcontec);
}
public void dialog(Bitmap resim, final Context ctx){
final Dialog dialog = new Dialog(ctx, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.perdereklam);
ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
reklam.setImageBitmap(resim);
reklam.setOnTouchListener(new OnTouchListener() {
// fill your stuff...
}
dialog.show();
}
calling from any class:
public static void showRateDialog(final Context mContextr) {
final Dialog dialog = new Dialog(mContextr, R.style.CustomDialogTheme);
ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
final DownloadImageTask dt = new DownloadImageTask(reklam, mContextr);
dt.execute("your url");
}
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 have an Async running to get data from a page I've created. It get's the text just fine, but when I try and get the image from the image src via another class the app force closes. Here is the code that it force closes on:
public class FullReportActivity extends NavigationActivity {
private TextView textView;
private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
private ImageView ivDangerRose;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// tell which region this covers
getSupportActionBar().setSubtitle("...from Sierra Avalanche Center");
setContentView(R.layout.activity_fullreport);
textView = (TextView) findViewById(R.id.todaysReport);
ivDangerRose = (ImageView) findViewById(R.id.dangerRose);
fetcher task = new fetcher();
task.execute();
}
// GET THE IMAGE and RETURN IT
public static Bitmap getBitmapFromURL(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;
}
}
class fetcher extends AsyncTask<String, Void, String> {
private ProgressDialog dialog = new ProgressDialog(
FullReportActivity.this);
private Document doc = null;
private Document parse = null;
private String results = null;
private String reportDate = null;
private Bitmap bimage = null;
#Override
protected String doInBackground(String... params) {
try {
doc = Jsoup.connect(url).get();
Log.e("Jsoup", "...is working...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Exception", e.getMessage());
}
parse = Jsoup.parse(doc.html());
results = doc.select("#fullReport").outerHtml();
Element dangerRoseImg = doc.getElementById("reportRose")
.select("img").first();
String dangerRoseSrc = dangerRoseImg.absUrl("src");
Log.i("Report Rose IMG", dangerRoseSrc);
bimage = getBitmapFromURL(dangerRoseSrc);
ivDangerRose.setImageBitmap(bimage);
return results;
}
#Override
protected void onPostExecute(String result) {
dialog.dismiss();
// smooth out the long scrolling...
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
reportDate = parse.select("#reportDate").outerHtml();
textView.setText(Html.fromHtml(reportDate + results));
textView.setPadding(30, 20, 20, 10);
}
#Override
protected void onPreExecute() {
dialog.setMessage("Loading Full Report from the Sierra Avalanche Center...");
dialog.show();
}
}
}
I have run this Async alone to get the image like so without a force close and I don't understand what i am doing different besides calling the method:
public class MainActivity extends Activity {
public String durl = "http://www.sierraavalanchecenter.org/dangerrose.png?a=2955";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadImageTask((ImageView) findViewById(R.id.dangerrose))
.execute(durl);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap drose = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
drose = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return drose;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
This class gets the image src and creates a bitmap and puts it into an ImageView, what is different here than on my first class???
Frustrated.
You can not modify UI from background thread.
move ivDangerRose.setImageBitmap(bimage); in onPostExecute
In the method doInBackground
remove --> ivDangerRose.setImageBitmap(bimage);
as you can't modify UI in background process.
If you still want you can try runOnUiThread Method
In doInBackground() we should not access the content of activity.
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);
}
}
}