Skip to content

Commit

Permalink
适配Android7.0 StrictMode
Browse files Browse the repository at this point in the history
fix:#16 Scheme 为file自动转换为Content Uri,开发者不用担心Scheme 为file 的Uri兼容问题
fix:#4
  • Loading branch information
crazycodeboy committed Sep 24, 2016
1 parent 8961bed commit 93aebf1
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 341 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.android.tools.build:gradle:2.2.0'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
1 change: 1 addition & 0 deletions simple/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<activity android:name=".SimpleFragmentActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"></activity>
<activity android:name=".ResultActivity"></activity>
</application>

</manifest>
93 changes: 84 additions & 9 deletions simple/src/main/java/com/jph/simple/CustomHelper.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.jph.simple;

import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;

import java.io.File;


/**
* - 支持通过相机拍照获取图片
Expand All @@ -25,11 +33,10 @@
* GitHub:https://github.com/crazycodeboy
* Eamil:crazycodeboy@gmail.com
*/
public class CustomHelper implements View.OnClickListener{
public class CustomHelper{
private View rootView;
private Button btnPickBySelect;
private Button btnPickByTake;
private RadioGroup rgCrop;
private RadioGroup rgCrop,rgCompress,rgFrom,rgCropSize,rgCropTool,rgShowProgressBar;
private EditText etCropHeight,etCropWidth,etLimit,etSize,etPx;
public static CustomHelper of(View rootView){
return new CustomHelper(rootView);
}
Expand All @@ -38,20 +45,88 @@ private CustomHelper(View rootView) {
init();
}
private void init(){
btnPickBySelect= (Button) rootView.findViewById(R.id.btnPickBySelect);
btnPickByTake= (Button) rootView.findViewById(R.id.btnPickByTake);
rgCrop= (RadioGroup) rootView.findViewById(R.id.rgCrop);
rgCompress= (RadioGroup) rootView.findViewById(R.id.rgCompress);
rgCropSize= (RadioGroup) rootView.findViewById(R.id.rgCropSize);
rgFrom= (RadioGroup) rootView.findViewById(R.id.rgFrom);
rgShowProgressBar= (RadioGroup) rootView.findViewById(R.id.rgShowProgressBar);
rgCropTool= (RadioGroup) rootView.findViewById(R.id.rgCropTool);
etCropHeight= (EditText) rootView.findViewById(R.id.etCropHeight);
etCropWidth= (EditText) rootView.findViewById(R.id.etCropWidth);
etLimit= (EditText) rootView.findViewById(R.id.etLimit);
etSize= (EditText) rootView.findViewById(R.id.etSize);
etPx= (EditText) rootView.findViewById(R.id.etPx);



}

@Override
public void onClick(View view) {
public void onClick(View view,TakePhoto takePhoto) {
File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
if (!file.getParentFile().exists())file.getParentFile().mkdirs();
Uri imageUri = Uri.fromFile(file);

configCompress(takePhoto);
switch (view.getId()){
case R.id.btnPickBySelect:
int limit= Integer.parseInt(etLimit.getText().toString());
if(limit>1){
if(rgCrop.getCheckedRadioButtonId()==R.id.rbCropYes){
takePhoto.onPickMultipleWithCrop(limit,getCropOptions());
}else {
takePhoto.onPickMultiple(limit);
}
return;
}
if(rgFrom.getCheckedRadioButtonId()==R.id.rbFile){
if(rgCrop.getCheckedRadioButtonId()==R.id.rbCropYes){
takePhoto.onPickFromDocumentsWithCrop(imageUri,getCropOptions());
}else {
takePhoto.onPickFromDocuments();
}
return;
}else {
if(rgCrop.getCheckedRadioButtonId()==R.id.rbCropYes){
takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions());
}else {
takePhoto.onPickFromGallery();
}
}
break;
case R.id.btnPickByTake:
if(rgCrop.getCheckedRadioButtonId()==R.id.rbCropYes){
takePhoto.onPickFromCaptureWithCrop(imageUri,getCropOptions());
}else {
takePhoto.onPickFromCapture(imageUri);
}
break;
default:
break;
}
}
private void configCompress(TakePhoto takePhoto){
if(rgCompress.getCheckedRadioButtonId()!=R.id.rbCompressYes)return ;
int maxSize= Integer.parseInt(etSize.getText().toString());
int maxPixel= Integer.parseInt(etPx.getText().toString());
boolean showProgressBar=rgShowProgressBar.getCheckedRadioButtonId()==R.id.rbShowYes? true:false;
CompressConfig config= new CompressConfig.Builder().setMaxPixel(maxSize).setMaxPixel(maxPixel).create();
takePhoto.onEnableCompress(config,showProgressBar);
}
private CropOptions getCropOptions(){
if(rgCrop.getCheckedRadioButtonId()!=R.id.rbCropYes)return null;
int height= Integer.parseInt(etCropHeight.getText().toString());
int width= Integer.parseInt(etCropWidth.getText().toString());
boolean withWonCrop=rgCropTool.getCheckedRadioButtonId()==R.id.rbCropOwn? true:false;

CropOptions.Builder builder=new CropOptions.Builder();

if(rgCropSize.getCheckedRadioButtonId()==R.id.rbAspect){
builder.setAspectX(width).setAspectY(height);
}else {
builder.setOutputX(width).setOutputY(height);
}
builder.setWithOwnCrop(withWonCrop);
return builder.create();
}

}
64 changes: 64 additions & 0 deletions simple/src/main/java/com/jph/simple/ResultActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.jph.simple;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.jph.takephoto.model.TImage;

import java.io.File;
import java.util.ArrayList;


/**
* - 支持通过相机拍照获取图片
* - 支持从相册选择图片
* - 支持从文件选择图片
* - 支持多图选择
* - 支持批量图片裁切
* - 支持批量图片压缩
* - 支持对图片进行压缩
* - 支持对图片进行裁剪
* - 支持对裁剪及压缩参数自定义
* - 提供自带裁剪工具(可选)
* - 支持智能选取及裁剪异常处理
* - 支持因拍照Activity被回收后的自动恢复
* Author: crazycodeboy
* Date: 2016/9/21 0007 20:10
* Version:3.0.0
* 技术博文:http://www.cboy.me
* GitHub:https://github.com/crazycodeboy
* Eamil:crazycodeboy@gmail.com
*/
public class ResultActivity extends Activity {
ArrayList<TImage>images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_layout);
images= (ArrayList<TImage>) getIntent().getSerializableExtra("images");
showImg();
}
private void showImg() {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llImages);
for (int i = 0, j = images.size(); i < j - 1; i += 2) {
View view = LayoutInflater.from(this).inflate(R.layout.image_show, null);
ImageView imageView1 = (ImageView) view.findViewById(R.id.imgShow1);
ImageView imageView2 = (ImageView) view.findViewById(R.id.imgShow2);
Glide.with(this).load(new File(images.get(i).getPath())).into(imageView1);
Glide.with(this).load(new File(images.get(i + 1).getPath())).into(imageView2);
linearLayout.addView(view);
}
if (images.size() % 2 == 1) {
View view = LayoutInflater.from(this).inflate(R.layout.image_show, null);
ImageView imageView1 = (ImageView) view.findViewById(R.id.imgShow1);
Glide.with(this).load(new File(images.get(images.size() - 1).getPath())).into(imageView1);
linearLayout.addView(view);
}

}
}
90 changes: 10 additions & 80 deletions simple/src/main/java/com/jph/simple/SimpleActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package com.jph.simple;

import android.net.Uri;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import com.bumptech.glide.Glide;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;


Expand All @@ -43,64 +33,17 @@
* Eamil:crazycodeboy@gmail.com
*/
public class SimpleActivity extends TakePhotoActivity {
private ToggleButton toggleButton;
private boolean withOwnCrop;

private CustomHelper customHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_layout);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
withOwnCrop = isChecked;
}
});
View contentView=LayoutInflater.from(this).inflate(R.layout.common_layout,null);
setContentView(contentView);
customHelper=CustomHelper.of(contentView);
}

public void cropPic(View view) {
File file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
Uri imageUri = Uri.fromFile(file);
CompressConfig compressConfig = new CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create();
CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(withOwnCrop).create();
TakePhoto takePhoto = getTakePhoto();
takePhoto.onEnableCompress(compressConfig, true);
switch (view.getId()) {
case R.id.btnPickFromGallery://从相册选择照片不裁切
takePhoto.onPickFromGallery();
break;
case R.id.btnPickFromGalleryWithCrop://从相册选择照片进行裁剪
takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
break;
case R.id.btnPickFromCapture://从相机拍取照片不裁剪
getTakePhoto().onPickFromCapture(imageUri);
break;
case R.id.btnPickFromCaptureWithCrop://从相机拍取照片进行裁剪
takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
break;
case R.id.btnPickFromDocuments://从文件选择照片不裁剪
takePhoto.onPickFromDocuments();
break;
case R.id.btnDocumentsCrop://从文件选择照片并裁剪
getTakePhoto().onPickFromDocumentsWithCrop(imageUri, cropOptions);
break;
case R.id.btnPickMultiple://图片多选
getTakePhoto().onPickMultiple(5);
break;
case R.id.btnPickMultipleCompress://图片多选并压缩
takePhoto.onPickMultiple(5);
break;
case R.id.btnPickMultipleCrop://图片多选并裁切
getTakePhoto().onPickMultipleWithCrop(5, cropOptions);
break;
case R.id.btnPickMultipleCropCompress://图片多选裁切并压缩
takePhoto.onPickMultipleWithCrop(5, cropOptions);
break;
default:
break;
}
public void onClick(View view) {
customHelper.onClick(view,getTakePhoto());
}

@Override
Expand All @@ -120,21 +63,8 @@ public void takeSuccess(TResult result) {
}

private void showImg(ArrayList<TImage> images) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llImages);
for (int i = 0, j = images.size(); i < j - 1; i += 2) {
View view = LayoutInflater.from(this).inflate(R.layout.image_show, null);
ImageView imageView1 = (ImageView) view.findViewById(R.id.imgShow1);
ImageView imageView2 = (ImageView) view.findViewById(R.id.imgShow2);
Glide.with(this).load(new File(images.get(i).getPath())).into(imageView1);
Glide.with(this).load(new File(images.get(i + 1).getPath())).into(imageView2);
linearLayout.addView(view);
}
if (images.size() % 2 == 1) {
View view = LayoutInflater.from(this).inflate(R.layout.image_show, null);
ImageView imageView1 = (ImageView) view.findViewById(R.id.imgShow1);
Glide.with(this).load(new File(images.get(images.size() - 1).getPath())).into(imageView1);
linearLayout.addView(view);
}

Intent intent=new Intent(this,ResultActivity.class);
intent.putExtra("images",images);
startActivity(intent);
}
}
Loading

0 comments on commit 93aebf1

Please sign in to comment.