0

i've an image processing app. If i take a picture using the part of the app that captures the image, the image is eventually displayed corectly on the screen. This is because i have specified camera params using the hardwares screen size. if i try to load an image that was taken from the phone's camera (not the app's camera), then the image is too large on the screen and sometimes too large from a byte point of view. how can i resize or compress the images in the gallery so that my app can use them? thanks

public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {

    private static final String TAG = "*********DUBFGActivity";
    private Context mContext = this;
    Uri uri;
    private Bitmap mbitmap = null;

    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


         uri = getIntent().getData();
        if(uri != null){
            Log.e(TAG, "uri ok");
        }else {
            Log.e(TAG, "uri not ok");
        }


        try {
              mbitmap = Media.getBitmap(getContentResolver(), uri);
             //setMbitmap(bitmap);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(mbitmap == null){
            Log.e(TAG,"mbitmap is null");
        }else{
            Log.e(TAG,"mbitmap is not null");
        }

.

public class DisplayUndistortedBitmapFromGallery extends View{





    public DisplayUndistortedBitmapFromGallery(Context context) {
        super(context);

    }





    public DisplayUndistortedBitmapFromGallery(Context context, AttributeSet attr) {
        super(context,attr);
        Log.e(TAG, "++++++++++ inside dubfgview constructor");





       Bitmap bm = ((DisplayUndistortedBitmapFromGalleryActivity)getContext()).getMbitmap();

        if(bm == null){
            Log.e(TAG, "bm = null");
        }else{
            Log.e(TAG, "bm =  not null");
        }


        bgr = bm.copy(bm.getConfig(), true);
turtleboy
  • 8,210
  • 27
  • 100
  • 199

1 Answers1

1

If I understand your question correctly there are two problems. The first is that the ImageView isn't scaling the image to fit the screen and the second is that the Bitmaps are requiring too much RAM.

To solve the first problem simply tell the image view how you want the image to be scaled. Simply do the following:

imgView.setScaleType(ImageView.ScaleType.INSERT_SCALE_TYPE);

Here is a list of the different scale types.

If this doesn't solve the problem make sure the bounds of your ImageView aren't too large. Try hardcoding the size of the ImageView to something specific and see if that fixes the problem.

The second problem is really easy to fix. You simply need to tell BitmapFactory to downsample the image when its loaded. Here is an example:

int inSample = 4; // specifies how many 1/N samples to keep, in this case 1/4 off all samples are kept 

opts = new BitmapFactory.Options();
opts.inSampleSize = inSample;

Bitmap b = BitmapFactory.decodeResource(c.getResources(), imgResId, opts);

For a more longer explanation see my answer to this question: Android GalleryView Recycling

Community
  • 1
  • 1
slayton
  • 20,123
  • 10
  • 60
  • 89
  • Hi, I don't have an imageview rather a custom view class. does this make a difference to the help you're providing? – turtleboy Nov 13 '11 at 15:58
  • @turtleboy only with regards to the `imgView.setScaleType()` method. The other stuff can still be the same. – slayton Nov 13 '11 at 23:05