0

enter image description here I dont know what happen , at first it is running now i have this error this is my code:

I dont know what happen , at first it is running now i have this error this is my code:

I dont know what happen , at first it is running now i have this error this is my code:

    public void allFile() {
    mTempDir = Environment.getExternalStorageDirectory()
            + "/MyCameraBooth/Photo/";

    File mTempFile = new File(mTempDir);
    if (!mTempFile.exists()) {
        mTempFile.mkdirs();
    }

    mBackground = Bitmap.createBitmap(1800, 1200, Bitmap.Config.RGB_565);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    File f = new File(AppConstant.filepathone);
    Bitmap mImageone = decodeFile(f);
    File g = new File(AppConstant.filepathtwo);
    Bitmap mImagetwo = decodeFile(g);
    File h = new File(AppConstant.filepaththree);
    Bitmap mImagethree = decodeFile(h);
    File i = new File(AppConstant.filepathfour);
    Bitmap mImagefour = decodeFile(i);

    Map<String, Bitmap> mBgMap = new HashMap<String, Bitmap>();
    mBgMap.put("Frame1", BitmapFactory.decodeResource(getResources(),
            R.drawable.frameone));
    mBgMap.put("Frame2", BitmapFactory.decodeResource(getResources(),
            R.drawable.frametwo));
    mBgMap.put("Frame3", BitmapFactory.decodeResource(getResources(),
            R.drawable.framethree));
    mBg = mBgMap.get(sharedpreferences.getString("getFrame", "getFrame"));

    Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
    Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360, true);
    Bitmap mImageth = Bitmap
            .createScaledBitmap(mImagethree, 515, 360, true);
    Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360, true);
    Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635, true);

    mCanvas = new Canvas(mBackground);
    mCanvas.drawARGB(255, 150, 150, 10);
    mCanvas.drawBitmap(mBack, 0, 0, null);
    mCanvas.drawBitmap(mImaget, 75, 75, null);
    mCanvas.drawBitmap(mImageo, 75, 490, null);
    mCanvas.drawBitmap(mImageth, 645, 75, null);
    mCanvas.drawBitmap(mImagef, 1215, 75, null);

    try {
        String friendlydate = DateFormat.getTimeInstance(DateFormat.MEDIUM)
                .format(new Date());
        friendlydate = friendlydate.replace(':', '_');
        String filename = friendlydate + ".jpg";
        mBitmapDrawable = new BitmapDrawable(mBackground);
        Bitmap mNewSaving = mBitmapDrawable.getBitmap();
        String FtoSave = mTempDir + filename;
        File mFile = new File(FtoSave);
        mFileOutputStream = new FileOutputStream(mFile);
        mNewSaving.compress(Bitmap.CompressFormat.PNG, 100,
                mFileOutputStream);

        imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(
                mFile.getAbsolutePath(), 1800, 1200));

        // imVCature_pic.setImageBitmap(decodeFile(mFile));
        mFileOutputStream.flush();
        mFileOutputStream.close();
        mFileOutputStream = null;

    } catch (FileNotFoundException e) {
        Log.e(AppConstant.TAG, "FileNotFoundExceptionError " + e.toString());
    } catch (IOException e) {
        Log.e(AppConstant.TAG, "IOExceptionError " + e.toString());
    }
    Log.i(AppConstant.TAG, "Image Created");

}


  private static Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 150;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        o.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {

    }

    return null;
}
Aiiz Cabuhat Ü
  • 149
  • 2
  • 17
  • 1
    possible duplicate of [Out of memory on a 9830416-byte allocation with bitmap](http://stackoverflow.com/questions/15335183/out-of-memory-on-a-9830416-byte-allocation-with-bitmap) – Harald K Dec 18 '14 at 13:54

1 Answers1

5

this is a classical Android problem. You're running out of memory while trying to allocate memory for a image. You can try two things to solve this.

the fast way: enable large heap in your application, to do this simple add android:largeHeap="true" to the application tag inside your android-manifest.xml. the problem with this approach is that large heap does not work with older android versions, if I remeber well you'll need at least android 3.0 to this work.

http://developer.android.com/guide/topics/manifest/application-element.html

the good way: Load a Scaled Down Version into Memory. You can check this link to see how to do this http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

but the main idea resides in this code:

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize 
        //value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}