When I zoom out an image too much, the app crashes.
LogCat:
*FATAL EXCEPTION MAIN:
java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java.2153)*
What I have to do???
If you are using ViewPager, then replace it with HackyViewPager.
If you have a problem in that case you can use bitmap rather than universal image loader.
Hope it helps
ImageView bmImage;
public DisplayImageFromURL(ImageView bmImage)
{
this.bmImage = 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.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
//pd.dismiss();
}
Related
Pretty new to Android development, haven't found the answer needed.
I have an ImageView some layout.xml:
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/placeholder"
android:layout_alignParentRight="true" />
Instead the placeholder, I want to load some specific image from url that i have.
How can it be achieved?
for download the image for url ,first download that image and saved it in Bitmap
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(),IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
Than you can set image by call that method
image.setImageBitmap(bitmap);
Hope it will help you
Use Picasso Library for android to download images from url and display onto ImageView
Its very easy to use like this
Picasso.with(context).load("http://your_image_url").into(your_imageView);
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 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);
}
}
And call from your onCreate() method using:
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(MY_URL_STRING);
Dont forget to add below permission in your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
you may try ImageLoader,you can download it here https://github.com/nostra13/Android-Universal-Image-Loader
I want to get some images (which are in .gif format) from a web page and then change an ImageView widget in my app.
I found the following code on stackoverflow:
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
Then, in the onPostExecute method, I have added the following line which doesn't work (although it doesn't give any visible error).
((ImageView) findViewById(R.id.imgWeather)).setImageDrawable(LoadImageFromWebOperations(pic_image)); //pic_image is the url which ends in .gif
Try this,
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 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);
}
}
and call it as,
new DownloadImageTask("//(ImageView) findViewById(R.id.imgWeather)")
.execute("//pic_image is the url which ends in .gif");
this is the code I'm using.
link contain image
http://mycapturetest.byethost17.com/Screen.jpg
when i update image on URl and open it on browser. it shows updated image.
but when open the same link in android application it keep showing the last image uploaded on the URl. (Not the new one)
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
bmImage.setImageDrawable(null);
bmImage.setImageBitmap(null);
}
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.setImageDrawable(null);
bmImage.setImageBitmap(null);
bmImage.setImageBitmap(result);
Toast.makeText(MainActivity.this, "set....", Toast.LENGTH_SHORT).show();
}
}
You can turn off caching of resources loaded from URLs using the URLConnection class:
try {
URLConnection connection = new java.net.URL(urldisplay).openConnection();
connection.setUseCaches(false);
InputStream in = connection.getInputStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
I need to load high res remote images to imageview, but without using any external libraries or frameworks. Images are like 2150x1500 px, and here is my code for loading:
URL imageUrl = new URL(url);
HttpURLConnection connection;
if (url.startsWith("https://")) {
connection = (HttpURLConnection) imageUrl.openConnection();
} else {
connection = (HttpURLConnection) imageUrl.openConnection();
}
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setInstanceFollowRedirects(true);
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(f);
CacheUtils.CopyStream(is, os);
os.close();
image = decodeFile(f);
img.setImageBitmap(image);
and here is decodeFile function:
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
and I always get texture size too big exception. Is there any way to load these images? Or is there a way to resize image not 2 or 4 times, but resize to fit 2048 pixels by width?
class DownloadImage extends AsyncTask<String, Void, Bitmap>
{
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
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) {
bmImage.setImageBitmap(Bitmap.createScaledBitmap(result, 120, 120, false));
}
}
Specify the height and width you need instead of 120.
Call this in your oncreate
new DownloadImage((ImageView) findViewById(R.id.imageView1))
.execute("https://.........");
I am using JSON to retrieve user data and I am also getting the image URL but how can I show the image instead of the URL? Currently it only shows the URL from the image.
Here is the code:
StringBuilder tweetResultBuilder = new StringBuilder();
try {
//get JSONObject from result
JSONObject resultObject = new JSONObject(result);
//get JSONArray contained within the JSONObject retrieved - "results"
JSONArray tweetArray = resultObject.getJSONArray("results");
//loop through each item in the tweet array
for (int t=0; t<tweetArray.length(); t++) {
//each item is a JSONObject
JSONObject tweetObject = tweetArray.getJSONObject(t);
//get the username and text content for each tweet
tweetResultBuilder.append(tweetObject.getString("from_user")+": ");
tweetResultBuilder.append(tweetObject.get("profile_image_url")+"\n");
tweetResultBuilder.append(tweetObject.get("text")+"\n\n");
}
}
You should download and cashe you image first.
Please read this article Displaying Bitmaps Efficiently, and check Sample application
The shorter way to display image from url:
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
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 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);
}
}
You need to download image from the url you are getting.
For that you need to do following things.
ImageView my_image = (ImageView) findViewById(R.id.my_imageView);
String image_url = "http://www.clker.com/cliparts/1/d/7/e/12570918991185627521Ramiras_Earth_small_icon.svg.med.png";
Bitmap bm = getImageFromServer(image_url);
my_image.setImageBitmap(bm);
This will call the following function to download image.
private Bitmap getImageFromServer(String image_url) {
Bitmap bm = null;
try {
URL aURL = new URL(image_url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
}
return bm;
}
That's it. Hope this helps you somehow.
Thanks.
try this one
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}