Skip to content

Commit

Permalink
添加对录制视频的大小限制
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzichen committed May 25, 2017
1 parent 1fc1e1c commit c649b7a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 31 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/hc/gallery/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void showRequestPermissionRationale(int requestCode, Rationale rationale)

GalleryHelper.with(MainActivity.this).type(GalleryConfig.RECORD_VEDIO).requestCode(12)
// .limitRecordTime(10)//限制时长
.limitRecordSize(1)//限制大小
.execute();
break;
case 5:/***拍照片onActivityResult {@link GalleryActivity.PHOTOS}*/
Expand Down
13 changes: 12 additions & 1 deletion gallery/src/main/java/com/dhc/gallery/GalleryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class GalleryConfig implements Parcelable {
private String filePath;
private int type = SELECT_PHOTO;
private int requestCode = -1;
private int limitRecordTime;
private int limitRecordTime;
private int limitRecordSize;


public GalleryConfig() {
Expand All @@ -40,6 +41,14 @@ public void setFilterMimeTypes(String[] filterMimeTypes) {
this.filterMimeTypes = filterMimeTypes;
}

public int getLimitRecordSize() {
return limitRecordSize;
}

public void setLimitRecordSize(int limitRecordSize) {
this.limitRecordSize = limitRecordSize;
}

public int getLimitRecordTime() {
return limitRecordTime;
}
Expand Down Expand Up @@ -130,6 +139,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.type);
dest.writeInt(this.requestCode);
dest.writeInt(this.limitRecordTime);
dest.writeInt(this.limitRecordSize);
}

protected GalleryConfig(Parcel in) {
Expand All @@ -143,6 +153,7 @@ protected GalleryConfig(Parcel in) {
this.type = in.readInt();
this.requestCode = in.readInt();
this.limitRecordTime = in.readInt();
this.limitRecordSize = in.readInt();
}

public static final Parcelable.Creator<GalleryConfig> CREATOR = new Parcelable.Creator<GalleryConfig>() {
Expand Down
25 changes: 17 additions & 8 deletions gallery/src/main/java/com/dhc/gallery/GalleryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GalleryHelper() {
static GalleryHelper instance;

/**
* 从哪里开启
* 开启页面
* @param mActivity
* @return
*/
Expand All @@ -48,7 +48,7 @@ public static GalleryHelper with(Activity mActivity) {
}

/**
* 从哪里开启
* 开启页面
* @param mFragment
* @return
*/
Expand All @@ -58,9 +58,9 @@ public static GalleryHelper with(Fragment mFragment ){
return instance;
}
/**
* 从哪里开启
* @param mFragment
* @return
* 开启页面
* @param mV4Fragment
* @return GalleryHelper
*/
public static GalleryHelper with(android.support.v4.app.Fragment mV4Fragment ){
instance = new GalleryHelper();
Expand All @@ -76,7 +76,7 @@ public static GalleryHelper with(android.support.v4.app.Fragment mV4Fragment ){
* {@link GalleryConfig.RECORD_VEDIO }
* {@link GalleryConfig.SELECT_VEDIO }
* {@link GalleryConfig.TAKEPHOTO_RECORDVEDIO }
* @return
* @return GalleryHelper
*/
public GalleryHelper type(int type) {
configuration.setType(type);
Expand All @@ -86,7 +86,7 @@ public GalleryHelper type(int type) {
/**
* 响应码
* @param type
* @return
* @return GalleryHelper
*/
public GalleryHelper requestCode(int type) {
configuration.setRequestCode(type);
Expand All @@ -96,12 +96,21 @@ public GalleryHelper requestCode(int type) {
/**
* 限定视频录制时间
* @param limitRecordTime
* @return
* @return GalleryHelper
*/
public GalleryHelper limitRecordTime(int limitRecordTime) {
configuration.setLimitRecordTime(limitRecordTime);
return this;
}
/**
* 限定视频录制大小
* @param limitRecordSize
* @return GalleryHelper
*/
public GalleryHelper limitRecordSize(int limitRecordSize) {
configuration.setLimitRecordSize(limitRecordSize);
return this;
}

/**
* 单选视频
Expand Down
53 changes: 36 additions & 17 deletions gallery/src/main/java/com/dhc/gallery/ui/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
Expand All @@ -40,6 +41,7 @@
import java.io.File;
import java.util.ArrayList;

import static android.content.ContentValues.TAG;
import static com.dhc.gallery.ui.GalleryActivity.GALLERY_CONFIG;

/**
Expand Down Expand Up @@ -75,7 +77,8 @@ public class CameraActivity extends BaseFragment implements NotificationCenter.N
private int[] viewPosition = new int[2];
private DecelerateInterpolator interpolator = new DecelerateInterpolator(1.5f);
private GalleryConfig mGalleryConfig;
BaseDialog mBaseDialog;
private BaseDialog mBaseDialog;
private File mFile;

public interface CameraActivityDelegate {

Expand Down Expand Up @@ -110,11 +113,11 @@ public void onFragmentDestroy() {
return;
}
hideCamera(true);
if(cameraView!=null){
cameraView.destroy(true, null);
FrameLayout frameLayout = (FrameLayout) fragmentView;
frameLayout.removeView(cameraView);
cameraView = null;
if (cameraView != null) {
cameraView.destroy(true, null);
FrameLayout frameLayout = (FrameLayout) fragmentView;
frameLayout.removeView(cameraView);
cameraView = null;
}
fragmentView = null;
if (mBaseDialog != null)
Expand Down Expand Up @@ -236,15 +239,24 @@ public void run() {
return;
}
if (mGalleryConfig.getLimitRecordTime() == 0) {

videoRecordTime++;

} else {
videoRecordTime--;
if (videoRecordTime == 0) {
if (vedioReleased())
return;
}
}
Log.d(TAG, "run " + mGalleryConfig.getLimitRecordSize());
if (mGalleryConfig.getLimitRecordSize() != 0) {
int b = getSize(cameraFile);
Log.d(TAG, "run " + b);
if (b > 0 && b >= mGalleryConfig.getLimitRecordSize() * 1024 * 1024) {
if (vedioReleased())
return;
}
}
//TODO 录制事件的限制
recordTime.setText(String.format("%02d:%02d", videoRecordTime / 60, videoRecordTime % 60));
AndroidUtilities.runOnUIThread(videoRecordRunnable, 1000);
Expand All @@ -263,7 +275,7 @@ public void onFinishVideoRecording(final Bitmap thumb) {
cameraPhoto.add(new MediaController.PhotoEntry(0, 0, 0, cameraFile.getAbsolutePath(), 0, true));
// Log.d("VV", "录制结束");

String msg = getVedioSize(cameraFile.getAbsolutePath());
String msg = getVedioSize(cameraFile);
mBaseDialog = getDialog(CameraActivity.this.getParentActivity(), msg, new PhotoViewer.EmptyPhotoViewerProvider() {


Expand Down Expand Up @@ -464,25 +476,32 @@ public void onAnimationEnd(Animator animator) {
}
}

private String getVedioSize(String absolutePath) {
File convertedFile = new File(absolutePath);
private String getVedioSize(File convertedFile) {
String message = "";
if (convertedFile.exists() && convertedFile.length() != 0 && CameraActivity.this.getParentActivity() != null) {
int b = (int) convertedFile.length();
int kb = b / 1024;
float mb = kb / 1024f;
message += mb > 1 ? CameraActivity.this.getParentActivity().getString(R.string.capture_video_size_in_mb, mb)
: CameraActivity.this.getParentActivity().getString(R.string.capture_video_size_in_kb, kb);
message += CameraActivity.this.getParentActivity().getString(R.string.is_send_video);
if (mb <= 1 && kb < 10) {
// FIXME: 2017/4/18
// TODO: 2017/4/7 视屏录制时间 过段 走着了 ,正式开发中去处理
return message;
if (b > mGalleryConfig.getLimitRecordSize() * 1024 * 1024) {
message += mb > 1 ? CameraActivity.this.getParentActivity().getString(R.string.over_video_size_in_mb, mb)
: CameraActivity.this.getParentActivity().getString(R.string.over_video_size_in_kb, kb);
} else {
message += mb > 1 ? CameraActivity.this.getParentActivity().getString(R.string.capture_video_size_in_mb, mb)
: CameraActivity.this.getParentActivity().getString(R.string.capture_video_size_in_kb, kb);
}

message += CameraActivity.this.getParentActivity().getString(R.string.is_send_video);
}
return message;
}

private int getSize(File convertedFile) {
if (!convertedFile.exists() && convertedFile.length() == 0)
return 0;
int b = (int) convertedFile.length();
return b;
}

private BaseDialog getDialog(Context context, String msg, final PhotoViewer.EmptyPhotoViewerProvider emptyPhotoViewerProvider) {
BaseDialog baseDialog = new BaseDialog(context)
.setCustomerContent(R.layout.dialog_layout)
Expand Down
10 changes: 5 additions & 5 deletions gallery/src/main/res/layout/dialog_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:textColor="@color/clr_000000"
android:text="需要写的信息"
android:textColor="#F88833"
android:textSize="20sp" />
android:textSize="18sp" />

<View
android:layout_width="match_parent"
Expand All @@ -33,8 +35,7 @@
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="#6FBF6A"
android:textSize="20sp" />
android:textSize="18sp" />

<View
android:layout_width="1dp"
Expand All @@ -48,7 +49,6 @@
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:textColor="#6FBF6A"
android:textSize="20sp" />
</LinearLayout>

Expand Down
2 changes: 2 additions & 0 deletions gallery/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
<string name="album_read_fail">相册读取失败,请检查权限</string>
<string name="capture_video_size_in_kb">视频文件大小为: %1$dKB,</string>
<string name="capture_video_size_in_mb">视频文件大小为: %1$.2fMB,</string>
<string name="over_video_size_in_kb">视频已超限,大小为: %1$dKB,</string>
<string name="over_video_size_in_mb">视频已超限,大小为: %1$.2fMB,</string>
<string name="is_send_video">是否发送该视频?</string>
</resources>
2 changes: 2 additions & 0 deletions gallery/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@

<string name="capture_video_size_in_kb">视频文件大小为: %1$dKB,</string>
<string name="capture_video_size_in_mb">视频文件大小为: %1$.2fMB,</string>
<string name="over_video_size_in_kb">视频已超限,大小为: %1$dKB,</string>
<string name="over_video_size_in_mb">视频已超限,大小为: %1$.2fMB,</string>
<string name="is_send_video">是否发送该视频?</string>
</resources>

0 comments on commit c649b7a

Please sign in to comment.