How download picture
-
Good evening. I try to download picture form url and insert in Image View. I use this code to do it:
public Bitmap getBitmapFromURL(String src) {
Bitmap myBitmap = null;
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}I just want to ask you, there is problem to do so, or is it inefficient? Closing this application, remains the downloaded picture remains in memory? If I want to free memory resource, how can I cancel this files (accumulated in time)?
-
Good evening. I try to download picture form url and insert in Image View. I use this code to do it:
public Bitmap getBitmapFromURL(String src) {
Bitmap myBitmap = null;
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}I just want to ask you, there is problem to do so, or is it inefficient? Closing this application, remains the downloaded picture remains in memory? If I want to free memory resource, how can I cancel this files (accumulated in time)?
Andy_Bell wrote:
Closing this application, remains the downloaded picture remains in memory?
Only until the Android OS removes the application from memory. You could save space by immediately saving the bitmap on disk and only loading it into memory when you want to display it. Resources will get freed when the Bitmap object goes out of scop or is otherwise disposed.
-
Andy_Bell wrote:
Closing this application, remains the downloaded picture remains in memory?
Only until the Android OS removes the application from memory. You could save space by immediately saving the bitmap on disk and only loading it into memory when you want to display it. Resources will get freed when the Bitmap object goes out of scop or is otherwise disposed.
-
Ok so unistalling my application, user can completly removed images downloaded. I have another question about. Where is the folder that contains those files?
Andy_Bell wrote:
Where is the folder that contains those files?
All depends where you save them. There was an excellent article about storage handling here on CodeProject, but the author has removed it. I guess you will need to do some searching for yourself to find something similar.
-
Andy_Bell wrote:
Where is the folder that contains those files?
All depends where you save them. There was an excellent article about storage handling here on CodeProject, but the author has removed it. I guess you will need to do some searching for yourself to find something similar.
I haven't save file, I use only code attached in previous post. What I've been doing is connecting with HttpURLConnection at url of the image, downloading file and reading input stream. I think, doing in this way, I have a temp copy of image file stored in Android folder, but I don't know what it is.Isn't it? Thank you!
-
I haven't save file, I use only code attached in previous post. What I've been doing is connecting with HttpURLConnection at url of the image, downloading file and reading input stream. I think, doing in this way, I have a temp copy of image file stored in Android folder, but I don't know what it is.Isn't it? Thank you!
-
Doing it this way the file only exists in virtual storage. As soon as the Bitmap object goes out of scope (e.g the object is disposed) then the data is destroyed.
-
Wonderful. Just another curiosity, if I can... If I start thread with Thread instance and Run method, how can I kill this and, if it's required, restarting when it's required. Can I use thread.destroy() ?
-
Wonderful. Just another curiosity, if I can... If I start thread with Thread instance and Run method, how can I kill this and, if it's required, restarting when it's required. Can I use thread.destroy() ?
I would be inclined to use
AsyncTask
instead. It's easily stoppable (without corrupting other areas)."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles