问题
I have strange problem - it occurs on xperia (android 4.0.3) but not on samsung S plus (2.3.6). In application i'm using google maps APIv2 with support library (in another activity. Not sure if matters).
I think i have narrowed problem to:
private Bitmap bitmap;
private String st;
private static final String PHOTO_DIR = "photodir";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
[...]
st = Environment.getExternalStorageDirectory().toString() + "/" + PHOTO_DIR + "/test.jpg";
bitmap = BitmapFactory.decodeFile(st);
ImageView im = (ImageView) findViewById(R.id.imv_Photo);
im.setImageBitmap(bitmap);
[...]
This works as expected. But this:
@Override
public void onClick(View v)
{
try
{
ImageView im = (ImageView) findViewById(R.id.imv_Photo);
String st = Environment.getExternalStorageDirectory().toString() + "/" + PHOTO_DIR + "/test1.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(st);
im.setImageBitmap(bitmap);
}
catch (Exception e)
{
// null
}
}
hangs the application (despite of try/catch block!) even with im.setImageBitmap(bitmap) removed. When i comment out BitmapFactory.decodeFile problem dissappears. Even worse xperia unmounts sd-card when plugged to usb and i don't know how to write pics to phone memory yet, so i'm unable to check log (btw: is there a way to get log later?)
When i remove code from onCreate(), onClick() works exactly once. I tried to set bitmap = null, and setImageBitmap(null) before decoding, but it doesn't help.
This seems very strange to me as samsung has no problem with same code.
Is there any solution or maybe i'm doing something wrong?
回答1:
Do one thing. Before passing to decode file Sample the image. Check the code below
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 3;
Bitmap bitmap = BitmapFactory.decodeFile(st,options);
im.setImageBitmap(bitmap);
来源:https://stackoverflow.com/questions/19513112/bitmapfactory-decodefile-and-imageview-strange-thing-in-android