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.
Related
background
suppose i have an inputStream that was originated from the internet of a certain image file.
i wish to get information about the image file and only then to decode it.
it's useful for multiple purposes, such as downsampling and also previewing of information before the image is shown.
the problem
i've tried to mark&reset the inputStream by wrapping the inputStream with a BufferedInputStream , but it didn't work:
inputStream=new BufferedInputStream(inputStream);
inputStream.mark(Integer.MAX_VALUE);
final BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(inputStream,null,options);
//this works fine. i get the options filled just right.
inputStream.reset();
final Bitmap bitmap=BitmapFactory.decodeStream(inputStream,null,options);
//this returns null
for getting the inputStream out of a url, i use:
public static InputStream getInputStreamFromInternet(final String urlString)
{
try
{
final URL url=new URL(urlString);
final HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
final InputStream in=urlConnection.getInputStream();
return in;
}
catch(final Exception e)
{
e.printStackTrace();
}
return null;
}
the question
how can i make the code handle the marking an resetting ?
it works perfectly with resources (in fact i didn't even have to create a new BufferedInputStream for this to work) but not with inputStream from the internet...
EDIT:
it seems my code is just fine, sort of...
on some websites (like this one and this one), it fails to decode the image file even after reseting.
if you decode the bitmap (and use inSampleSize) , it can decode it fine (just takes a long time).
now the question is why it happens, and how can i fix it.
I believe the problem is that the call to mark() with the large value is overwritten by a call to mark(1024). As described in the documentation:
Prior to KITKAT, if is.markSupported() returns true, is.mark(1024) would be called. As of KITKAT, this is no longer the case.
This may be resulting in a reset() fail if reads larger than this value are being done.
(Here is a solution for the same problem, but when reading from disk. I didn't realize at first your question was specifically from a network stream.)
The problem with mark & reset in general here is that BitmapFactory.decodeStream() sometimes resets your marks. Thus resetting in order to do the actual read is broken.
But there is a second problem with BufferedInputStream: it can cause the entire image to be buffered in memory along side of where ever you are actually reading it into. Depending on your use case, this can really kill your performance. (Lots of allocation means lots of GC)
There is a really great solution here:
https://stackoverflow.com/a/18665678/1366
I modified it slightly for this particular use case to solve the mark & reset problem:
public class MarkableFileInputStream extends FilterInputStream
{
private static final String TAG = MarkableFileInputStream.class.getSimpleName();
private FileChannel m_fileChannel;
private long m_mark = -1;
public MarkableFileInputStream( FileInputStream fis )
{
super( fis );
m_fileChannel = fis.getChannel();
}
#Override
public boolean markSupported()
{
return true;
}
#Override
public synchronized void mark( int readlimit )
{
try
{
m_mark = m_fileChannel.position();
}
catch( IOException ex )
{
Log.d( TAG, "Mark failed" );
m_mark = -1;
}
}
#Override
public synchronized void reset() throws IOException
{
// Reset to beginning if mark has not been called or was reset
// This is a little bit of custom functionality to solve problems
// specific to Android's Bitmap decoding, and is slightly non-standard behavior
if( m_mark == -1 )
{
m_fileChannel.position( 0 );
}
else
{
m_fileChannel.position( m_mark );
m_mark = -1;
}
}
}
This won't allocate any extra memory during reads, and can be reset even if the marks have been cleared.
whether you can mark / reset a stream depends on the implementation of the stream. those are optional operations and aren't typically supported. your options are to read the stream into a buffer and then read from that stream 2x, or just make the network connection 2x.
the easiest thing is probably to write into a ByteArrayOutputStream,
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int count;
byte[] b = new byte[...];
while ((count = input.read(b) != -1) [
baos.write(b, 0, count);
}
now either use the result of baos.toByteArray() directly, or create a ByteArrayInputStream and use that repeatedly, calling reset() after consuming it each time.
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
that might sound silly, but there's no magic. you either buffer the data in memory, or you read it 2x from the source. if the stream did support mark / reset, it'd have to do the same thing in it's implementation.
Here is a simple method that always works for me :)
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
image = BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return image;
}
I have a big problem with parsing some json data which I get as response from a web server. The thing that I'm doing is I get the response via POST and than convert the response as string and parse it. But in some devices I get OutOfMemoryError , which I'm trying to fix. Here is how I'm converting the response to string :
public static String convertStreamToString(InputStream is) throws Exception {
ByteArrayOutputStream into = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int n; 0 < (n = is.read(buf));) {
into.write(buf, 0, n);
}
into.close();
return new String(into.toByteArray(), "UTF-8");
}
and here is how I'm using this piece of code :
InputStream response = new BufferedInputStream(connection.getInputStream());
try {
String responsee = convertStreamToString(response);
jsonParser(responsee);
} catch (Exception e) {
e.printStackTrace();
cancelDialog("Error occurred! Please try again later.");
}
Any suggestions how can I fix that problem so don't happen in all devices?
Thanks in advance for any kind of help or advices.
The mobile has limited internal memory.
I have also face same issue. The solution that we found is that download only the necessary information. So you please confirm your requirement how much data you want to inside the mobile. If you filter the unnecessary data then the problem will get resolved.
Before testing the program on extreme condition first check whether simple download happening if it is happening then check the limit of your data means up to how extent it will not give out of memory error. and accordingly that rework your requirement.
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.
I have an android application i have been working on that downloads an image from a server, reads it into a bitmap and displays it on an ImageView
This works great most of the time, but every so often, it goes through the process (There is a ProgressDialog saying "Fetching image...") and once its done nothing gets displayed. There has not been anything in logcat that even seems to remotely relate to this.
Here is the code:
Bitmap image = null;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(webService + "?cmd=get");
try
{
HttpResponse resp = client.execute(get);
Log.i("PhotoRouletteDebug", "Resp buffer size: " + (int)resp.getEntity().getContentLength());
InputStream is = resp.getEntity().getContent();
BufferedInputStream buf = new BufferedInputStream(is, (int)resp.getEntity().getContentLength());
image = BitmapFactory.decodeStream(buf);
// clean up
buf.close();
is.close();
Even when nothing is getting displayed, the Resp content length always reports a correct size but still, nothing ends up getting displayed.
This code is called from an AsyncTask, but only 1 task is ever called at a time.
This is driving me insane, i have no idea why its keeps doing this.
Edit: Here is the code that sets the imageView
// AsyncTask for Getting a new image from the queue
protected class GetImageTask extends AsyncTask<String, String, Bitmap>
{
protected void onPreExecute()
{
// lets show a progress dialog so the user knows something is going on
progressDialog = ProgressDialog.show(PhotoRoulette.this, "", "Fetching image...", true);
}
protected void onPostExecute (Bitmap image)
{
// we got a new photo so lets display it where it needs to be displayed
try
{
photoView = (ImageView)findViewById(R.id.photoView);
photoView.setImageBitmap(image);
}
catch (Exception e)
{
Log.e("Debug", "Something absolutely retarded happened", e);
}
// hide the progress dialog - we're all done
progressDialog.dismiss();
}
protected Bitmap doInBackground(String... urls)
{
// Get a new Bitmap Queue Image
Bitmap image = imageHandler.getQueueImage();
return image;
}
}
You didn't show us the code for displaying the image, so we don't know for sure that that code is correct. Perhaps the problem lies there?
But assuming that the problem is that the image is getting corrupted, here's how I'd start debugging this: Wrap buf with a PushbackInputStream. Read the bytes out of buf and save them to a file; then push those same bytes back into the PushbackInputStream. Then pass the PushbackInputStream into BitmapFactory.decodeStream. If the image is displayed successfully, then delete the file (manually or programatically.) Otherwise, you can now examine the bitmap at your leisure.
I am working on an application that downloads images from a url. The problem is that only some images are being correctly downloaded and others are not.
First off, here is the problem code:
public Bitmap downloadImage(String url) {
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(new HttpGet(url));
} catch (ClientProtocolException cpe) {
Log.i(LOG_FILE, "client protocol exception");
return null;
} catch (IOException ioe) {
Log.i(LOG_FILE, "IOE downloading image");
return null;
} catch (Exception e) {
Log.i(LOG_FILE, "Other exception downloading image");
return null;
}
// Convert images from stream to bitmap object
try {
Bitmap image = BitmapFactory.decodeStream(response.getEntity().getContent());
if(image==null)
Log.i(LOG_FILE, "image conversion failed");
return image;
} catch (Exception e) {
Log.i(LOG_FILE, "Other exception while converting image");
return null;
}
}
So what I have is a method that takes the url as a string argument and then downloads the image, converts the HttpResponse stream to a bitmap by means of the BitmapFactory.decodeStream method, and returns it. The problem is that when I am on a slow network connection (almost always 3G rather than Wi-Fi) some images are converted to null--not all of them, only some of them. Using a Wi-Fi connection works perfectly; all the images are downloaded and converted properly.
Does anyone know why this is happening? Or better, how can I fix this? How would I even go about testing to determine the problem? Any help is awesome; thank you!
This is a known issue with the JPEG decoder. There are two solutions. Either you download the entire image in a byte[] array using a ByteInputStream and then decode the array (this is what I do in code.google.com/p/shelves.) Another solution is to create a wrapper InputStream as shown below:
public class PatchInputStream extends FilterInputStream {
public PatchInputStream(InputStream in) {
super(in);
}
public long skip(long n) throws IOException {
long m = 0L;
while (m < n) {
long _m = in.skip(n-m);
if (_m == 0L) break;
m += _m;
}
return m;
}
}
Even with a WiFi connection some bitmaps (in particular .BMPs) will not be decoded. It's just a buggy decoder that can not deal with delays. If you search stackoverflow you will find some other solutions such as wrapping the HTTP stream in a buffered http entity. That works but depending on image size can take up a lot of memory. For our commerical product we ended up downloading the http stream to sdcard and then using Bitmapfactory on the dowloaded file. It's somewhat slower but 100% reliable.