2013-02-04 16 views
2

我有2個活動 活動A = GameActivity使用PhotoTask.class的一個實例,它擴展了AsyncTask並用於拍攝和保存圖片。一旦圖片被保存,我就開始活動B = ShareActivity。Android:內存錯誤,因爲ImageView?

當我拍照並且不改變方向時,活動B被調用並且一切正常。然而,如果我改變屏幕的方向來拍攝照片(我從肖像轉到風景),那麼當我拍照時(活動B沒有開始),我有錯誤。

我甚至在我的代碼中評論了我的位圖圖片,但仍然存在位圖錯誤! 什麼是奇怪是,如果我在我的代碼(它沒有做任何特別的)評論ImageView5並保留我的位圖,我沒有錯誤。這個問題似乎來自這ImageView但我不能解釋它

我添加了configChanges:清單中的方向,回收我的bnitmaps,使用一個活動的WeakReference而不是Acitvity本身作爲PhotoTask中的變量,試圖看到與Eclipse內存分析器(Android: Detecting leaks with Eclipse Memory Analyzer),但內存泄漏不明白爲什麼我有這些錯誤

我真的搜查了很多,我已經相當絕望,所以如果你能幫我,這將是很好

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

您的信息,我的代碼片段

public class ShareActivity extends Activity implements OnClickListener{ 
    public static final String APP_ID = "**********"; 
    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; 
    } 


} 

我imageview5

<ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="85dp" 
     android:layout_height="100dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginTop="20dp" 
     android:adjustViewBounds="true" 
     android:src="@drawable/diviseur" /> 
+0

您必須將其解碼。然後應用到圖像視圖 – itsrajesh4uguys

+0

在manifiestiest.xml文件什麼是你的屬性下面的屬性? android:screenOrientation = – itsrajesh4uguys

+0

這不是問題,因爲當我評論我把位圖放在imageview中的部分時,我仍然有錯誤 android.view.InflateException:二進制XML文件行#23:錯誤膨脹類 (我imageview5的地方),而我什麼也沒做! 我還沒有指定ScreenOrientation,但我有 android:configChanges =「orientation | keyboardHidden for my 2 activities – morg

回答

0

後你拍照,你需要使用圖片大小調整來調整圖像傳遞給它的圖像和新的寬度和高度,是你想要的。 如果微量元素對clcik DDMS堆,然後在evice設備列表和調試代碼,你會看到圖像的大小讀取,你可以給嘗試後Loading large bitmap Efficiently從公文

增加關節外普通 拿來看這下面的代碼,你的問題將得到解決

/** getResizedBitmap method is used to Resized the Image according to custom width and height 
    * @param image 
    * @param newHeight (new desired height) 
    * @param newWidth (new desired Width) 
    * @return image (new resized image) 
    * */ 
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, 
      matrix, false); 
    return resizedBitmap; 
} 
+0

查看我的評論 」我認爲這不是問題,因爲當我評論我放置位圖的部分在imageview中,我仍然有錯誤android.view.InflateException:二進制XML文件行#23:錯誤膨脹類(我的imageview5是),而我什麼也沒做!「 – morg

0

由於烏斯曼說,庫爾德人,你加載(全)圖像在內存中,並在內存中創建一個副本。這使用了大量的內存。該文件,他指的是美國:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
String imageType = options.outMimeType; 

你的記憶了對其中一行如果使用此機器人將不會加載的發生,而不是在imageview的

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 

設置圖像整個位圖到內存中,但縮小版本。 閱讀文檔,你的錯誤一定會消失。

上面的代碼只會解碼邊界。 documentation解釋瞭如何使用它。請參閱「將縮小版本加載到內存中」一章如何正確執行此操作