I have an image that is 8 mp but it can't be displayed on an ImageView because it is too large of a file. How can I re-size it with a BitmapFactory and then pass this new image as the source of ImageView in the XML?
my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select" />
<Button
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info" />
</LinearLayout>
<ImageView
android:id="@+id/test_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
My Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView view;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8; //also tried 32, 64, 16
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, options);
Drawable d = new BitmapDrawable(bmp);
view = (ImageView) findViewById(R.id.test_image_view);
view.setImageDrawable(d);
setContentView(R.layout.main);
}
You can't pass it into XML
For what? Load it, when you need it. In your case it's better to use BitmapFactory.Options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=16; //image will be 16x smaller than an original. Note, that it's better for perfomanse to use powers of 2 (2,4,8,16,32..).
Bitmap bmp=BitmapFactory.decodeSomething(..., options) //use any decode method instead of this
Drawable d=new BitmapDrawable(bmp);
and then just set it to your ImageView
ImageView iv; //initialize it first, of course. For example, using findViewById.
iv.setImageDrawable(d);
Note, that you should recycle your bitmap, after you used it (after ImageView is not visible anymore):
bmp.recycle();