Unable to clear or set blank image in imageview in android - android

if(bigimageS.length()==0){
show_image.setImageBitmap(null);
}else{
show_image.setImageBitmap(decodeImage(bigimageS));
}
****************
public static Bitmap decodeImage(String arrayList_image) {
BufferedInputStream bis;
URL aURL;
try{
aURL = new URL(arrayList_image);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
this is my code and i want to clear imageview.my problem is i am getting the image in bigimages String.i am adding the imageurl from w/s.in string bigimages.when i click on next btn after once image set then old image is not clear.so plz help i put condition but not working.plz help.
I am using the decodeimage for decode the image so is it any way to clear if there is no url in string..help me plz not solve yet

create a blank /transparent png image from photoshop , and apply as your image background.

You need to check the length of the url . If it is 0 then you have to set null to imageviwe.
Please try this.
if(imageneel.get(i).length() == 0)
{
show_image.setImageBitmap(null);
}

try this one code
for (int i = 0; i < imageneel.size(); i++) {
if(imageneel.get(i).trim().length() == 0){
show_image.setImageBitmap(null);
}else{
show_image.setImageBitmap(decodeImage(imageneel.get(i)));
}
}

Agree with Pranav, but i think you can also access inbuild resources of Android itself.
Try:
imgView.setImageResource(android.R.color.transparent);

finally i got the solution..i have simply set the blank like this bigimageS="";
before getdata(); method..

It is simple as passing a 0 as the resource
show_image.SetImageResource(0);

Related

I am facing java.net.MalformedURLException: no protocol: While converting url to bitmap in android

I am trying to convert url to bitmap and then set that bitmap to background as wallpaper. And all this process is getting done in background with the use of worker class in android. But I am getting no protocol error. I am fetching any one random wallpaper link from firebase then I am trying to convert it into bitmap by
try {
URL url = new URL(image_url);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But this method gave me this error in logcat
2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol:
But when I put hardcoded string (showed in below method) in url method parameter then it return bitmap without any error. and wallpaper also gets changed
try {
URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But i want random string to set and get me bitmap from that. So how i can achieve that? I also try to encode url by
try {
encodedURL = URLEncoder.encode(image_url, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("tag3",e.toString());
}
I am using three kind of url in this project
https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800
https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
https://images.unsplash.com/photo-1641414315243-196e7382c32d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1178&q=80
Any help will be appreciated.

what happen when memory is full in image gallery

there is a image gallery app which download images online. I want to know what will happen if phone memory/SD card get full; If an error will appear then what should I do?
This catches an OutofMemory error, but should check for other exceptions, too
URL aURL = null;
Bitmap bm = null;
try {
final String imageUrl = CMSServer + url;
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = new BufferedInputStream(conn.getInputStream());
is = new BufferedInputStream(conn.getInputStream());
bm = BitmapFactory.decodeStream(is, null, options);
is.close();
} catch (OutOfMemory oom) {
// do something
}
}
return bm;
}
The analogy can be made what if a class cannot adjust more students ?
So according to me :-
1. you can follow "Precaution is better than cure"
first of all you will check the sdcard itself and if its say >1MB then you will get the image.
Even if the error comes you can prompt user to delete older images to allow new images like we used to have older phones where msg memory was limited and it will ask to remove older messages to allow new messages.
Still if you get the error then perhaps i cannot exactly figure out what to do except showing the "Out of memory exception" to user.
Thanks and hope it helps.... :)
It will through IOException if no memory is left in sdcard. But if you know the size of file which you are downloading then you can check for sufficient memory and thus can avoid IOException.
Edit: Catch Exception
try
{
//your code here
}
catch(IOException ex)
{
//do stuff when exception caused.
}
if you don't know how to use/catch exception. Please google it.

why cannot load multiple image into list view?

I am downloading the image from website and attach into listview.
URL aURL;
try {
aURL = new URL(//"http://www.orientaldaily.com.my/"+
imagepath[i]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
imageview = (ImageView) findViewById(R.id.image_alllatestnewstitle);
imageview.setVisibility(View.VISIBLE);
imageview.setScaleType(ScaleType.CENTER_CROP);
imageview.setImageBitmap(bm);
} catch (IOException e) {
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
When i downloading only 1 image, it got no problem, however when i downloading multiple or more than 5 images and load into listview, it cause problem.
The problem was
bitmap size exceeds VM budget
How to avoid this problem?
Note: this is not duplicate any question!
Thanks.
Load lot many images causes the app to run out of memory and force closes.I think this is what happening to your application.the memory issue is a complex issue android while developing an application.this can be solved by manually clearing the unused bitmaps and by using the garbage collector.
Try using System.gc();
Try recycling the bitmap using
Bitmap.recycle();
Make all the unused bitmap null.
Deallocate all the unused memory.
This all will help you a lot and also go through this link.Use memory analyzer it will help you spot out the Deallocated memory>try this link
public void deAllocateAllMemory()
{
try
{
mGallery.imgLoader1.disposeImages();
unbindDrawables(findViewById(R.id.magazineGrid));
mGallery=null;
back.getBackground().setCallback(null);
back.setOnClickListener(null);
store.getBackground().setCallback(null);
store.setOnClickListener(null);
quickAction.setOnActionItemClickListener(null);
settings.getBackground().setCallback(null);
settings.setOnClickListener(null);
}
catch (Exception e)
{
}
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
try {
view.getBackground().setCallback(null);
((BitmapDrawable) view.getBackground()).getBitmap().recycle();
view.destroyDrawingCache();
view.notifyAll();
} catch (Exception e) {
}
}
This piece of code may help you a lil bit.
Displaying Bitmaps Efficiently tutorial could help you.

Can I test "drawable" taken from Url Stream for being a proper icon?

Im working on caching for images of my listview.
I have used the solution from here
The problem im having is - how to set a default image if icon does not exist?
The url to images is returning a server page that the image does not exist on the server. So im guessing that this html page is then somehow converted to faulty Drawable -> so that IOexception does not happen (coz some data is returned).
Can I test "drawable" for being a proper icon?.
or maybe my logic is false here. Please advise
Code from ListViewAdapter :
try {
URL url = new URL("http:// some image url here.jpg");
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Drawable drawable = Drawable.createFromStream(connection.getInputStream(), "src");
viewHolder.icon.setImageDrawable(drawable);
} catch (IOException e) {
viewHolder.icon.setImageResource(R.drawable.defaulticon);
}
Your drawable variable will be null if it can't be decoded from stream. Check for null and set your default image.
Drawable drawable = null;
try {
URL url = new URL("http:// some image url here.jpg");
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
// try to load and decode
drawable = Drawable.createFromStream(connection.getInputStream(), "src");
if(drawable != null) // if succeed - set drawable
viewHolder.icon.setImageDrawable(drawable);
} catch (IOException e) {
// ignore IOException if any...
} finally {
if(drawable == null) // If drawable are null (not decoded or failed to load (404?))
viewHolder.icon.setImageResource(R.drawable.defaulticon); // set default image
}

ListView scrolling lag in android

I am having a customized list view in my application, which is showing an image and text.
The Image I am getting from URL, using the code below:
private static Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
all is working perfect, except the list view scrolling, its very slow. If I disable the images, the scroll speed smooth-ens , but with the image enabled, it lags alot.
Is there any possible way I can reduce or remove this lagging?
Thanks
You need to do your fetching in the background.
One of the examples you can use :
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
use this library for downloading images in background and caching..
it won't hurt the UI
https://github.com/koush/UrlImageViewHelper
Are you lazy loading your images? See this question for details.

Categories

Resources