拍照与摄像头的Andr​​oid(意向)内存不足的错误 [英] Take picture with android camera (intent) out of memory error

查看:154
本文介绍了拍照与摄像头的Andr​​oid(意向)内存不足的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个烦恼与下面code。它只是拍照的onclick使用摄像头的Andr​​oid的意图,并将其显示在ImageView的图像。

  1. 在两个或三个画面不留活动结束后,将其与内存不足错误崩溃往往当我旋转显示。
  2. 当我拍摄照片第一次,也刷新ImageView的,但是当我做第二次或第三次......它不会刷新照片,直到我旋转屏幕
  3. 我想保存的图片在内部储存空间,而不是外在的,但我不知道该怎么办,因为我试了几个教程,它stucks相机!

    公共类HandScryActivity延伸活动{

     私有静态诠释TAKE_PICTURE = 1;
    私人MtgMatch myMatch;
    私人文件handFile;
    
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.handscry);
        //关闭屏幕保护程序
        。getWindow()addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
        //负载匹配
        myMatch = MtgMatch.getSingletonMtgMatch();
        handFile =新的文件(Environment.getExternalStorageDirectory(),test.jpg放在);
        如果(myMatch.getHandUri()!= NULL){的LoadPicture(); }
    }
    
    @覆盖
    保护无效onRestoreInstanceState(包savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        的LoadPicture();
    }
    
    //把手onGame点击按钮
    公共无效btnHandClick(视图v){
            按钮clickedButton =(按钮)V;
        根据点击按钮//
        开关(clickedButton.getId()){
            案例R.id.btnBackToGame:
                this.finish();
                打破;
            案例R.id.btnTakePicture:
                myMatch.setHandUri(Uri.fromFile(handFile));
                意向意图=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,myMatch.getHandUri());
                startActivityForResult(意向,TAKE_PICTURE);
                打破;
            默认:
                打破;
        }
    }
    
    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
            如果(要求code == TAKE_PICTURE){
                //显示图片
                如果(结果code == RESULT_OK){
                    的LoadPicture();
            }否则,如果(结果code == RESULT_CANCELED){
                //用户取消了图像采集
            } 其他 {
                //图像捕捉失​​败,提醒用户
            }
        }
    }
    
    //把里面的帧照片
    私人无效的LoadPicture(){
            ImageView的IMG =(ImageView的)findViewById(R.id.imgHand);
            img.setImageURI(myMatch.getHandUri());
    }
    
    }
     

解决方案

您有内存泄漏。究其原因旋转屏幕导致内存消耗殆尽是因为屏幕旋转自动销毁活动并重建它。您可以通过覆盖的onPause和ONSTART方法,并把调试语句中他们证明了这一点,然后旋转屏幕,你看到他们被调用。你需要了解Android的活动生命周期。

您有内存泄漏,因为你保持引用这些图像在内存中。您需要进行跟踪内存使用情况。当你倾斜屏幕,旧的活动坚持围绕在内存中,并创建一个新的。为了使垃圾收集器,收集你的不必要的对象,你必须确保没有留给他们在code参考。有工具来绘制应用程序的内存使用情况,所以你可以找出内存泄漏是:

按照指示在本页面有MAT告诉你在哪里内存泄漏是:

在Android的内存分析工具?

I'm having two troubles with the below code. It just take picture "onclick" using intent of camera android and it display the image on the ImageView.

  1. After two or three pictures without leaving the activity, it crash with an outOfMemory error often when i'm rotating the display.
  2. When I take picture first time, it refresh the imageview but when i do second or third time...it doesn't refresh the picture until I rotate the screen
  3. I would like to save picture on internal storage instead of external, but I don't understand how to do cause I tried several tutorial and it stucks the camera!

    public class HandScryActivity extends Activity {

    private static int TAKE_PICTURE = 1;
    private MtgMatch myMatch;
    private File handFile;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handscry);  
        // Disable screen saver
        getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
        // Load match 
        myMatch = MtgMatch.getSingletonMtgMatch();
        handFile = new File(Environment.getExternalStorageDirectory(), "test.jpg");
        if (myMatch.getHandUri() != null) { loadPicture(); }        
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        loadPicture();
    }
    
    // Handles onGame clicked buttons
    public void btnHandClick(View v) {
            Button clickedButton = (Button) v;
        // according to clicked button
        switch (clickedButton.getId()) {
            case R.id.btnBackToGame:
                this.finish();
                break;
            case R.id.btnTakePicture:              
                myMatch.setHandUri(Uri.fromFile(handFile));
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                
                intent.putExtra(MediaStore.EXTRA_OUTPUT, myMatch.getHandUri());
                startActivityForResult(intent, TAKE_PICTURE);
                break;              
            default:
                break;
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {                     
            if (requestCode == TAKE_PICTURE) {
                // Display image
                if (resultCode == RESULT_OK) {
                    loadPicture();
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }
    }
    
    // Put the photo inside frame
    private void loadPicture() {
            ImageView img = (ImageView) findViewById(R.id.imgHand);
            img.setImageURI(myMatch.getHandUri());
    }
    
    }
    

解决方案

You have a memory leak. The reason rotating your screen is causing memory exhaustion is because a screen rotate automatically destroys the Activity and rebuilds it. You can prove this by overriding the onPause and onStart methods and placing a debug statement in them, then rotate the screen and you see them get invoked. You need to understand the android Activity Lifecycle.

You have memory leaks because you are keeping references to those images in memory. You need to be tracking the memory usage. When you tilt the screen, the old activity is sticking around in memory, and a new one is created. In order for the garbage collector to collect up your unnecessary objects, you have to make sure there are no references left to them in the code. There are tools to chart the memory usage of your application so you can find out where the memory leak is:

Follow the directions in this page to have MAT tell you where the memory leak is:

Memory Analyzer Tool in android?

这篇关于拍照与摄像头的Andr​​oid(意向)内存不足的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆