Skip to content

Commit

Permalink
feat: add take photo
Browse files Browse the repository at this point in the history
  • Loading branch information
Qixingchen committed Jan 20, 2017
1 parent b267e2a commit 8dd79c5
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-beta1'
classpath 'com.android.tools.build:gradle:2.3.0-beta2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.4.0'
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.5.0'
Expand All @@ -27,8 +27,8 @@ task clean(type: Delete) {
}

ext {
supportLibraryVersion = '25.0.1'
supportLibraryVersion = '25.1.0'
rx_binding_version = '0.4.0'
sdk_version = 24
buildToolsVersion = "25.0.0"
sdk_version = 25
buildToolsVersion = "25.0.1"
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Mon Jan 16 09:22:57 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
17 changes: 15 additions & 2 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="moe.xing.getimage">

<application
>
<uses-feature
android:name="android.hardware.camera"
android:required="false"/>

<application>
<activity
android:name=".GetImageActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

<activity android:name="com.soundcloud.android.crop.CropImageActivity"/>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/get_image_provider"/>
</provider>

</application>

</manifest>
62 changes: 59 additions & 3 deletions lib/src/main/java/moe/xing/getimage/GetImageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.widget.Toast;

import com.soundcloud.android.crop.Crop;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import moe.xing.baseutils.Init;
Expand Down Expand Up @@ -40,6 +45,8 @@ public class GetImageActivity extends Activity {
private static final String SELECT_MODE = "select_mode";
private static final String MAX_SIZE = "max_size";
private Uri corpedImage;
@Nullable
private File takenFile = null;

/**
* 获取启动 intent
Expand All @@ -60,18 +67,27 @@ public static Intent getStartIntent(Context context, int subscriberID, @RxGetIma
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
doSelect();
doGet();
}
}

@Override
protected void onNewIntent(Intent intent) {
doSelect();
doGet();
}

/**
* 选择图片
*/
private void doGet() {
int mode = getIntent().getIntExtra(SELECT_MODE, RxGetImage.MODE_SINGLE);
if (mode == RxGetImage.MODE_TAKE_PHOTO || mode == RxGetImage.MODE_TAKE_PHOTO_AND_CORP) {
doTake();
} else {
doSelect();
}
}

private void doSelect() {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
Expand All @@ -81,6 +97,25 @@ private void doSelect() {
IntentUtils.startIntentForResult(photoPickerIntent, this, SELECT_PHOTO);
}

private void doTake() {
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
takenPhotoFile();
} catch (IOException ioe) {
RxGetImage.getInstance().onError(new Throwable("无法创建相片,可能空间已满"), getSubscriberID());
finish();
return;
}
if (takenFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
takenFile);
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
IntentUtils.startIntentForResult(takeIntent, this, TAKE_PHOTO);
}

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Expand All @@ -103,6 +138,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
finish();
}
break;
case TAKE_PHOTO:
if (takenFile != null && takenFile.exists() && takenFile.length() > 0) {
if (RxGetImage.MODE_TAKE_PHOTO_AND_CORP == getSelectMode()) {
toCorp(takenFile);
} else {
RxGetImage.getInstance().onAns(takenFile, getSubscriberID());
RxGetImage.getInstance().onComplete(getSubscriberID());
}
} else {
RxGetImage.getInstance().onError(new Throwable("空文件"), getSubscriberID());
finish();
}
default:
super.onActivityResult(requestCode, resultCode, data);
finish();
Expand Down Expand Up @@ -223,7 +270,6 @@ public void onNext(File s) {
});
}


private int getSubscriberID() {
return getIntent().getIntExtra(SUBSCRIBER_ID, 0);
}
Expand All @@ -250,4 +296,14 @@ private void toCorp(File image) {
}
corpedImage = null;
}

private File takenPhotoFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";

File image = FileUtils.getCacheFile(timeStamp);
takenFile = image;

return image;
}
}
42 changes: 37 additions & 5 deletions lib/src/main/java/moe/xing/getimage/RxGetImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class RxGetImage {
public static final int MODE_SINGLE = 1;
public static final int MODE_SINGLE_AND_CORP = 2;
public static final int MODE_MULTIPLE = 3;
public static final int MODE_TAKE_PHOTO = 4;
public static final int MODE_TAKE_PHOTO_AND_CORP = 5;
private static RxGetImage sSingleton;
private SparseArray<Subscriber<? super File>> mSubscribers = new SparseArray<>();

Expand Down Expand Up @@ -51,7 +53,7 @@ public static RxGetImage getInstance() {
* @return Observable<File>
*/
@NonNull
public Observable<File> getImage(@SelectMode final int selectMode) {
public Observable<File> getImage(final boolean needCorp) {
return Observable.create(new Observable.OnSubscribe<File>() {
@Override
public void call(Subscriber<? super File> subscriber) {
Expand All @@ -61,7 +63,8 @@ public void call(Subscriber<? super File> subscriber) {
i++;
}

Intent intent = GetImageActivity.getStartIntent(Init.getApplication(), i, selectMode, 1);
Intent intent = GetImageActivity.getStartIntent(Init.getApplication(), i,
needCorp ? MODE_SINGLE_AND_CORP : MODE_SINGLE, 1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Init.getApplication().startActivity(intent);
mSubscribers.append(i, subscriber);
Expand Down Expand Up @@ -95,6 +98,31 @@ public void call(Subscriber<? super File> subscriber) {
});
}

/**
* 拍摄图片
*
* @return Observable<File>
*/
@NonNull
public Observable<File> takeImage(final boolean needCorp) {
return Observable.create(new Observable.OnSubscribe<File>() {
@Override
public void call(Subscriber<? super File> subscriber) {
synchronized (RxGetImage.class) {
int i = 1;
while (mSubscribers.get(i) != null) {
i++;
}
Intent intent = GetImageActivity.getStartIntent(Init.getApplication(), i,
needCorp ? MODE_TAKE_PHOTO_AND_CORP : MODE_TAKE_PHOTO, 1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Init.getApplication().startActivity(intent);
mSubscribers.append(i, subscriber);
}
}
});
}

/**
* 设置返回的图片
*
Expand All @@ -114,15 +142,19 @@ void onAns(@Nullable File file, int subscriberID) {
*/
void onError(Throwable message, int subscriberID) {
Subscriber<? super File> subscriber = mSubscribers.get(subscriberID);
subscriber.onError(message);
if (subscriber != null) {
subscriber.onError(message);
}
}

void onComplete(int subscriberID) {
Subscriber<? super File> subscriber = mSubscribers.get(subscriberID);
subscriber.onCompleted();
if (subscriber != null) {
subscriber.onCompleted();
}
}

@IntDef({MODE_SINGLE, MODE_MULTIPLE, MODE_SINGLE_AND_CORP})
@IntDef({MODE_SINGLE, MODE_MULTIPLE, MODE_SINGLE_AND_CORP, MODE_TAKE_PHOTO, MODE_TAKE_PHOTO_AND_CORP})
public @interface SelectMode {
}
}
9 changes: 9 additions & 0 deletions lib/src/main/res/xml/get_image_provider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path
name="my_images_external"
path=""/>
<cache-path
name="my_images"
path=""/>
</paths>

0 comments on commit 8dd79c5

Please sign in to comment.