1

I have 2 activities Activity A = GameActivity uses an instance of PhotoTask.class which extends AsyncTask and is used to take and save picture. Once the picture is saved, I start Activity B = ShareActivity.

When I take a picture and don't change the orientation, Activity B is called and everything works fine. Yet, if I change the orientation of the screen to take the picture (I pass from portait to landscape) I can't start the Activity B and I have errors.

02-01 16:09:36.571: E/AndroidRuntime(13362): FATAL EXCEPTION: main
02-01 16:09:36.571: E/AndroidRuntime(13362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kersplatt/com.example.kersplatt.ShareActivity}: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown>
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.os.Looper.loop(Looper.java:130)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at java.lang.reflect.Method.invoke(Method.java:507)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at dalvik.system.NativeStart.main(Native Method)
02-01 16:09:36.571: E/AndroidRuntime(13362): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown>
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.Activity.setContentView(Activity.java:1657)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at com.example.kersplatt.ShareActivity.onCreate(ShareActivity.java:64)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-01 16:09:36.571: E/AndroidRuntime(13362):    ... 11 more
02-01 16:09:36.571: E/AndroidRuntime(13362): Caused by: java.lang.reflect.InvocationTargetException
02-01 16:09:36.571: E/AndroidRuntime(13362):    at java.lang.reflect.Constructor.constructNative(Native Method)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-01 16:09:36.571: E/AndroidRuntime(13362):    ... 23 more
02-01 16:09:36.571: E/AndroidRuntime(13362): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.widget.ImageView.<init>(ImageView.java:118)
02-01 16:09:36.571: E/AndroidRuntime(13362):    at android.widget.ImageView.<init>(ImageView.java:108)
02-01 16:09:36.571: E/AndroidRuntime(13362):    ... 26 more

I even commented by bitmap images in my code, but still have bitmap errors!

I added configChanges:orientation in the manifest, recycle my bnitmaps, used a WeakReference of an activity instead of the acitvity itself as a variable in PhotoTask, tried to see the memory leaks with eclipse memory analyzer (Android: Detecting leaks with Eclipse Memory Analyzer) but didnt understand why I had these errors

I really searched a lot and I'm becoming quite desperate so if you could please help me it would be nice

FOR YOUR INFORMATION, PIECE OF MY CODE

public class ShareActivity extends Activity implements OnClickListener{
    public static final String APP_ID = "477110419013909";
    Facebook facebook = new Facebook(APP_ID);
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    String FILENAME = "AndroidSSO_data";
    SharedPreferences mPrefs;
    Handler mHandler;
    File file;
    byte[] bytes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.w("SHAREACTIVITY","SECOND");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share);
        file = (File) this.getIntent().getExtras().get("PICTURE_TAKEN");
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        ImageView image5 = (ImageView) findViewById(R.id.imageView5);
        image5.setImageBitmap(mutableBitmap);
        bitmap.recycle();
        mutableBitmap.recycle();

    }

CLASS PHOTOTASK

public class PhotoTask extends AsyncTask<Boolean, Void, Void> {
    private Camera camera;
    private SurfaceView surfaceCamera;
    private boolean isPreview=false;
    private SurfaceHolder holder;
    private WeakReference<GameActivity> weakgameactivity;
    GameActivity gameactivity;
    private File output;

    public PhotoTask(Camera camera, SurfaceView surfaceCamera,boolean isPreview, SurfaceHolder holder, GameActivity gameactivity) {
        super();
        this.camera = camera;
        this.surfaceCamera = surfaceCamera;
        this.isPreview = isPreview;
        this.holder = holder;
        this.weakgameactivity = new WeakReference<GameActivity>(gameactivity);
        this.gameactivity=this.weakgameactivity.get();
    }


    PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.w("GAMEACTIVITY","FIRST");
//          ImageView image5 = (ImageView) gameactivity.findViewById(R.id.imageView6);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/KersplattFolder");

            imagesFolder.mkdirs(); 
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
            String date = dateFormat.format(new Date());
            String fileName = "Kersplatt_" + date + ".jpg";
            output = new File(imagesFolder, fileName);
            ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView6);
            view.setDrawingCacheEnabled(true);
            Bitmap b = view.getDrawingCache();
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(output);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            b.compress(CompressFormat.JPEG, 95, fos);
            try {
            fos.write(data);
            fos.close();
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
            }
              catch (IOException e) {
                    e.printStackTrace();
            }
              camera.stopPreview();
              Log.w("GAMEACTIVITY","INTENT");
              b.recycle();
              Intent intent = new Intent(gameactivity.getApplicationContext(),ShareActivity.class);
              // Sending the picture taken to ShareActivity
              intent.putExtra("PICTURE_TAKEN", output);
              gameactivity.startActivity(intent);
        }

    };

    @Override
    protected Void doInBackground(Boolean... params) {
         camera.takePicture(null,null, myPictureCallback_JPG);
            Log.w("GAMEACTIVITY","TAKEPICTURE");
            return null;
    }


}
Community
  • 1
  • 1
morg
  • 1,173
  • 4
  • 18
  • 36
  • seems like it comes from you xml layout file => /AndroidRuntime(13362): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class – CinetiK Feb 01 '13 at 16:08
  • @CinetiK, the Stack trace continues, it doesn't stop there. So his exception is caused by the OOM exception. – tolgap Feb 01 '13 at 16:17
  • Infact, when I comment the ImageView line29 the Activity B opens. Do you know what Inflate Exception is? Basically I put a random image in this image view and when I take the picture from activity A I pass the picture in this image view. Maybe the problem is I don't resize the photo taken while the imageview's size are much more smaller? (but its find when i dont change orientation) – morg Feb 01 '13 at 16:21
  • Is there an error message in the logcat output right above the exception? Sometimes you'll get a very informative error message before the throw happens (e.g. it might say how big the bitmap was, which is helpful to tell if it's a straw breaking the camel's back or if it's just plain huge). – fadden Feb 01 '13 at 18:37

1 Answers1

0

Two things are likely happening.

Since you say that the problem only occurs when changing orientations, this suggests that your activity is being held in memory. Some reference (static, or otherwise out-of-sync with your activity's lifecycle) may be keeping the GC from cleaning up the old instance before the newly for the changed orientation can be inflated.

Secondly, you're trying to punch the entire photograph into an ImageView. This is a bad idea, because the photograph is likely huge and will consume a whole lot of memory. It may even cause OOMEs without the above problem. Consider decoding the bitmap at a lower sample size, as illustrated in this example.

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • I agree, but without seeing more of ShareActivity, it's hard to know. For example, is there an onDestroy, onPause, etc? It seems possible that the bitmap in image5 could possibly be held for several GC cycles in one Activity incarnation while the rotate incarnation is also being constructed. No silver bullets here, but [Android Issue 8488](http://code.google.com/p/android/issues/detail?id=8488) was an instructive read on similar problems. – RonU Feb 02 '13 at 05:38
  • Actually, come to think of it, it's more likely that the first PhotoTask is still running when the second is executed. The VM runs out of memory because the bitmap is being processed twice concurrently. – Paul Lammertsma Feb 02 '13 at 10:29