I need to add a new functionality into my app. I must store a lot of JPG images from internet into the Phone.
If the phone haves sdcard, the JPG files must be stored on the sdcard. If not, they must be stored on the internalMemory, only if the phone haves space to do it.
Then, i should do these things:
/Magazine/
I know how to get a JPG file from internet and how to transform it into BITMAP, but it is the only think i know, i dont know how to store it as JPG and i dont know the other things.
public static Bitmap getRemoteBitmap(String url) {
Bitmap bm=null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
new org.apache.http.auth.AuthScope(null,-1),
new org.apache.http.auth.UsernamePasswordCredentials(MagazineStatus._username, MagazineStatus._password));
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
bm=BitmapFactory.decodeStream(content );
}catch(Exception ex) {Log.e("DBF Error",ex.toString());}
}else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(ClientProtocolException cpe) {Log.e("ClientProtocolException @ at FPT",cpe.toString());} catch(Exception ex) {Log.e("Exception at FETCHPROJECTASK",ex.toString());}
return bm;
}
I'm searching in google how to do the things i need, but i can't find any clear information, i prefeer to ask you for the best way to do this, the optimal way.
THanks
So the return value of above method gives you bitmap from internet.
Use below code to save it as JPG to sdcard.
try {
bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/Magazine/Image001.jpg")));
// bm is your decoded bitmap from internet
} catch (FileNotFoundException e) {
e.printStackTrace()
}
I hope it may help you.