Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决内存泄漏问题 #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/src/main/java/com/cjt2325/cameraview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import android.widget.Toast;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.lisenter.JCameraLisenter;
import com.cjt2325.cameralibrary.listener.JCameraListener;

import java.io.File;

Expand All @@ -35,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
jCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");

//JCameraView监听
jCameraView.setJCameraLisenter(new JCameraLisenter() {
jCameraView.setJCameraLisenter(new JCameraListener() {
@Override
public void captureSuccess(Bitmap bitmap) {
//获取图片bitmap
Expand All @@ -48,11 +48,11 @@ public void recordSuccess(String url, Bitmap firstFrame) {
}


@Override
public void quit() {
//退出按钮
MainActivity.this.finish();
}
// @Override
// public void quit() {
// //退出按钮
// MainActivity.this.finish();
// }
});
//6.0动态权限获取
getPermissions();
Expand Down
36 changes: 31 additions & 5 deletions camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import com.cjt2325.cameralibrary.view.CameraView;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
Expand Down Expand Up @@ -113,6 +117,7 @@ public class JCameraView extends FrameLayout implements CameraInterface.CameraOp

private boolean firstTouch = true;
private float firstTouchLength = 0;
private ThreadPoolExecutor mPoolExecutor;

public JCameraView(Context context) {
this(context, null);
Expand All @@ -124,7 +129,12 @@ public JCameraView(Context context, AttributeSet attrs) {

public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
/**
* 使用弱引用的方式,避免造成内存泄漏
*/
WeakReference<Context> contextWeakReference = new WeakReference<>(context);
mContext = contextWeakReference.get();

//get AttributeSet
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.JCameraView, defStyleAttr, 0);
iconSize = a.getDimensionPixelSize(R.styleable.JCameraView_iconSize, (int) TypedValue.applyDimension(
Expand All @@ -136,6 +146,12 @@ public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) {
iconRight = a.getResourceId(R.styleable.JCameraView_iconRight, 0);
duration = a.getInteger(R.styleable.JCameraView_duration_max, 10 * 1000); //没设置默认为10s
a.recycle();

/**
* 使用线程池,避免内存泄漏
*/
mPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(128));

initData();
initView();
}
Expand Down Expand Up @@ -301,12 +317,13 @@ public void onPause() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
LogUtil.i("JCameraView SurfaceCreated");
new Thread() {
Runnable runnable = new Runnable() {
@Override
public void run() {
CameraInterface.getInstance().doOpenCamera(JCameraView.this);
}
}.start();
};
mPoolExecutor.execute(runnable);
}

@Override
Expand Down Expand Up @@ -361,6 +378,8 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_UP:
firstTouch = true;
break;
default:
break;
}
return true;
}
Expand Down Expand Up @@ -435,6 +454,8 @@ public void resetState(int type) {
case TYPE_DEFAULT:
mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
break;
default:
break;
}
mSwitchCamera.setVisibility(VISIBLE);
mFlashLamp.setVisibility(VISIBLE);
Expand Down Expand Up @@ -462,6 +483,8 @@ public void confirmState(int type) {
break;
case TYPE_DEFAULT:
break;
default:
break;
}
mCaptureLayout.resetCaptureLayout();
}
Expand All @@ -484,7 +507,7 @@ public void showPicture(Bitmap bitmap, boolean isVertical) {
public void playVideo(Bitmap firstFrame, final String url) {
videoUrl = url;
JCameraView.this.firstFrame = firstFrame;
new Thread(new Runnable() {
Runnable runnable = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void run() {
Expand Down Expand Up @@ -519,7 +542,8 @@ public void onPrepared(MediaPlayer mp) {
e.printStackTrace();
}
}
}).start();
};
mPoolExecutor.execute(runnable);
}

@Override
Expand Down Expand Up @@ -594,6 +618,8 @@ private void setFlashRes() {
mFlashLamp.setImageResource(R.drawable.ic_flash_off);
machine.flash(Camera.Parameters.FLASH_MODE_OFF);
break;
default:
break;
}
}
}