2

我有 2 个活动 Activity A = GameActivity 使用 PhotoTask.class 的一个实例,它扩展了 AsyncTask 并用于拍摄和保存图片。图片保存后,我启动 Activity B = ShareActivity。

当我拍照并且不改变方向时,会调用 Activity B 并且一切正常。然而,如果我改变屏幕的方向来拍照(我从纵向转到横向),我在拍照时会出错(活动 B 没有启动)。

我什至在我的代码中注释了我的位图图像,但仍然有位图错误!奇怪的是,如果我在我的代码中注释ImageView5(它没有做任何特别的事情)并保留我的位图,我没有错误。问题似乎来自这个 ImageView 但我无法解释

我在清单中添加了 configChanges:orientation,回收了我的 bnitmap,使用了活动的 WeakReference 而不是活动本身作为 PhotoTask 中的变量,尝试使用 Eclipse 内存分析器查看内存泄漏(Android:使用 Eclipse 内存分析器检测泄漏)但不明白为什么我有这些错误

我真的搜索了很多,我变得非常绝望,所以如果你能帮助我,那就太好了

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();

    }

课堂摄影

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" />
4

2 回答 2

0

正如 Usman Kurd 所说,您将(完整)图像加载到内存中,并在内存中创建该图像的副本。这会占用大量内存。他提到的文件指出:

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;

您的内存不足发生在这些行中的任何一条上,而不是在图像视图中设置图像时

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

如果您使用这个 android 不会将整个位图加载到内存中,而是按比例缩小版本。阅读文档,您的错误肯定会消失。

上面的代码只会解码边界。文档解释了如何使用它。请参阅名为“将按比例缩小的版本加载到内存中”的章节如何正确实现这一点

于 2013-02-07T08:42:38.003 回答
0

拍照后,您需要使用 Image resize 来调整图像大小,将其传递给 Image 以及您想要的新宽度和高度。如果在 clcik DDMS 上跟踪堆,然后在设备列表上的 evice 并调试代码,您将看到图像的大小增加额外的普通查看从官方文档有效地加载大位图

阅读后您可以尝试以下代码,您的问题将得到解决

 /** 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;
}
于 2013-02-04T10:09:09.937 回答