Skip to content

Commit 8f8cdb7

Browse files
authored
Merge pull request #42 from miguelbcr/hotfixes/#34-Crash_when_opening_camera_on_7.0+_devices
Hotfixes/#34 crash when opening camera on 7.0+ devices
2 parents 7b81720 + b427fa0 commit 8f8cdb7

6 files changed

Lines changed: 71 additions & 30 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016 FuckBoilerplate
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.fuck_boilerplate.rx_paparazzo.interactors;
18+
19+
class Constants {
20+
static final String SUBDIR = "RxPaparazzo";
21+
static final String SHOOT_APPEND = "shoot.jpg";
22+
static final String CROP_APPEND = "cropped.jpg";
23+
/**
24+
* The same name that is placed on the manifest on <code>provivder</code> tag
25+
*/
26+
static final String FILE_PROVIDER = "file_provider";
27+
static final String DATE_FORMAT = "ddMMyyyy_HHmmss_SSS";
28+
static final String LOCALE_EN = "en";
29+
}

rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/CropImage.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import rx.functions.Func2;
3333

3434
public final class CropImage extends UseCase<Uri> {
35-
private static final String SUBDIR = "RxPaparazzo";
36-
private static final String CROP_APPEND = "cropped.jpg";
3735
private final Config config;
3836
private final StartIntent startIntent;
3937
private final GetPath getPath;
@@ -72,7 +70,7 @@ public Uri call(Intent intentResult) {
7270
return Observable.just(uri);
7371
}
7472

75-
public Observable<Intent> getIntent() {
73+
private Observable<Intent> getIntent() {
7674
return Observable.zip(getInputUri(), getOutputUri(),
7775
new Func2<Uri, Uri, Intent>() {
7876
@Override
@@ -113,9 +111,9 @@ public Uri call(String filePath) {
113111

114112
private Observable<Uri> getOutputUri() {
115113
Context context = targetUi.getContext();
116-
File dir = new File(context.getFilesDir(), SUBDIR);
114+
File dir = new File(context.getFilesDir(), Constants.SUBDIR);
117115
dir.mkdirs();
118-
File file = new File(dir, CROP_APPEND);
116+
File file = new File(dir, Constants.CROP_APPEND);
119117
return Observable.just(Uri.fromFile(file).buildUpon().build());
120118
}
121119
}

rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/DownloadImage.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@
1616

1717
package com.fuck_boilerplate.rx_paparazzo.interactors;
1818

19+
import android.content.Context;
1920
import android.net.Uri;
2021

2122
import com.fuck_boilerplate.rx_paparazzo.entities.TargetUi;
22-
import com.fuck_boilerplate.rx_paparazzo.entities.UserCanceledException;
2323

2424
import java.io.BufferedInputStream;
2525
import java.io.File;
2626
import java.io.InputStream;
2727
import java.net.URL;
2828
import java.net.URLConnection;
29-
import java.util.concurrent.TimeUnit;
3029

3130
import rx.Observable;
3231
import rx.Subscriber;
3332
import rx.android.schedulers.AndroidSchedulers;
34-
import rx.exceptions.Exceptions;
35-
import rx.functions.Func0;
3633
import rx.schedulers.Schedulers;
3734

3835
public final class DownloadImage extends UseCase<String> {
@@ -60,9 +57,8 @@ private Observable<String> getObservableDownloadFile() {
6057
public void call(Subscriber<? super String> subscriber) {
6158
try {
6259
if ("content".equalsIgnoreCase(uri.getScheme())){
63-
subscriber.onNext(downloadContent());
64-
}
65-
else {
60+
subscriber.onNext(getContent());
61+
} else {
6662
subscriber.onNext(downloadFile());
6763
}
6864

@@ -82,15 +78,20 @@ private String downloadFile() throws Exception {
8278
URLConnection connection = url.openConnection();
8379
connection.connect();
8480
InputStream inputStream = new BufferedInputStream(url.openStream(), 1024);
85-
File file = imageUtils.getOutputFile();
86-
81+
Context context = targetUi.getContext();
82+
File dir = new File(context.getFilesDir(), Constants.SUBDIR);
83+
dir.mkdirs();
84+
File file = new File(dir, uri.getLastPathSegment() + ".tmp");
8785
imageUtils.copy(inputStream, file);
8886
return file.getAbsolutePath();
8987
}
9088

91-
private String downloadContent() throws Exception {
89+
private String getContent() throws Exception {
9290
InputStream inputStream = targetUi.getContext().getContentResolver().openInputStream(uri);
93-
File file = imageUtils.getOutputFile();
91+
Context context = targetUi.getContext();
92+
File dir = new File(context.getFilesDir(), Constants.SUBDIR);
93+
dir.mkdirs();
94+
File file = new File(dir, uri.getLastPathSegment() + ".tmp");
9495
imageUtils.copy(inputStream, file);
9596
return file.getAbsolutePath();
9697
}

rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/ImageUtils.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
import rx.exceptions.Exceptions;
4343

4444
public final class ImageUtils {
45-
private static final String DATE_FORMAT = "ddMMyyyy_HHmmss_SSS";
46-
private static final String LOCALE_EN = "en";
4745
private final TargetUi targetUi;
4846
private final Config config;
4947

@@ -53,11 +51,22 @@ public ImageUtils(TargetUi targetUi, Config config) {
5351
}
5452

5553
public File getOutputFile() {
56-
String filename = new SimpleDateFormat(DATE_FORMAT, new Locale(LOCALE_EN)).format(new Date());
57-
filename = "IMG-" + filename + ".jpg";
5854
String dirname = getApplicationName(targetUi.getContext());
5955
File dir = getDir(null, dirname);
60-
return new File(dir.getAbsolutePath(), filename);
56+
return getFile(dir);
57+
}
58+
59+
private File getFile(File dir) {
60+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT, new Locale(Constants.LOCALE_EN));
61+
String datetime = simpleDateFormat.format(new Date());
62+
File file = new File(dir.getAbsolutePath(), "IMG-" + datetime + ".jpg");
63+
64+
while (file.exists()) {
65+
datetime = simpleDateFormat.format(new Date());
66+
file = new File(dir.getAbsolutePath(), "IMG-" + datetime + ".jpg");
67+
}
68+
69+
return file;
6170
}
6271

6372
private String getApplicationName(Context context) {

rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/SaveImage.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.fuck_boilerplate.rx_paparazzo.entities.Config;
2323
import com.fuck_boilerplate.rx_paparazzo.entities.TargetUi;
2424

25+
import java.io.File;
26+
2527
import rx.Observable;
2628
import rx.functions.Func1;
2729
import rx.functions.Func3;
@@ -53,12 +55,15 @@ public Observable<String> react() {
5355
.flatMap(new Func1<Uri, Observable<String>>() {
5456
@Override
5557
public Observable<String> call(Uri outputUri) {
56-
return Observable.zip(getPath.with(uri).react(),
57-
getPath.with(outputUri).react(), getDimens.with(uri).react(),
58+
return Observable.zip(
59+
getPath.with(uri).react(),
60+
getPath.with(outputUri).react(),
61+
getDimens.with(uri).react(),
5862
new Func3<String, String, int[], String>() {
5963
@Override
6064
public String call(String filePath, String filePathOutput, int[] dimens) {
6165
String filePathScaled = imageUtils.scaleImage(filePath, filePathOutput, dimens);
66+
new File(filePath).delete();
6267
MediaScannerConnection.scanFile(targetUi.getContext(), new String[]{filePathScaled}, new String[]{"image/jpeg"}, null);
6368

6469
return filePathScaled;

rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/TakePhoto.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
public final class TakePhoto extends UseCase<Uri> {
3737
private static final int READ_WRITE_PERMISSIONS = Intent.FLAG_GRANT_READ_URI_PERMISSION
3838
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
39-
private static final String SUBDIR = "RxPaparazzo";
40-
private static final String SHOOT_APPEND = "shoot.jpg";
4139
private final StartIntent startIntent;
4240
private final TargetUi targetUi;
4341

44-
public TakePhoto(StartIntent startIntent, TargetUi targetUi) {
42+
public TakePhoto(StartIntent startIntent, TargetUi targetUi) {
4543
this.startIntent = startIntent;
4644
this.targetUi = targetUi;
4745
}
@@ -60,11 +58,12 @@ public Uri call(Intent data) {
6058

6159
private Uri getUri() {
6260
Context context = targetUi.getContext();
63-
File dir = new File(context.getFilesDir(), SUBDIR);
61+
File dir = new File(context.getFilesDir(), Constants.SUBDIR);
6462
dir.mkdirs();
65-
File file = new File(dir, SHOOT_APPEND);
63+
File file = new File(dir, Constants.SHOOT_APPEND);
6664

67-
return FileProvider.getUriForFile(context, context.getPackageName() + ".file_provider", file);
65+
String authority = context.getPackageName() + "." + Constants.FILE_PROVIDER;
66+
return FileProvider.getUriForFile(context, authority, file);
6867
}
6968

7069
private Intent getIntentCamera(Uri uri) {
@@ -93,7 +92,7 @@ private void grantFileReadWritePermissions(Intent intent, Uri uri) {
9392
}
9493
}
9594

96-
public void revokeFileReadWritePermissions(Uri uri) {
95+
private void revokeFileReadWritePermissions(Uri uri) {
9796
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
9897
targetUi.getContext().revokeUriPermission(uri, READ_WRITE_PERMISSIONS);
9998
}

0 commit comments

Comments
 (0)