I am trying to download an image from server and save it in the internal storage of android. It is working well in all cases if I am downloading an image less than 2 MB. But when I download any JPEG image of size greater than 2 MB, I am facing two issues.
First one is that when I am trying to download and save the image from server using the emulator, I am getting an error like "bitmap size exceeds VM budget" and activity crashes.
Second issue is that this problem is not their when I run the application on my phone, but instead of that another issue is happening. When I am displaying the downloaded image, the image contains some other colours also(kind of rainbow colours).
Here is the code that I am using to download and save the image:
//For downloading the image file
void downloadFile(String fileUrl)
{
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//For saving the downloaded image
void saveImage() {
try
{
String fileName ="displayimg.png";
final FileOutputStream fos = openFileOutput(fileName, Activation.MODE_PRIVATE);
bmImg.compress(CompressFormat.PNG, 90, fos);
} catch (Exception e)
{
e.printStackTrace();
}
}
Here is the code for displaying the image:
private Bitmap myBitmap;
myBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivDisplayImage.setImageBitmap(myBitmap);