Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 8191731

Browse files
author
Cornette Nicolas
committed
Improve example app: add photo shoot option
1 parent 8c9bfac commit 8191731

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

example/src/main/AndroidManifest.xml

+6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
</intent-filter>
1616
</activity>
1717
<activity android:name="com.soundcloud.android.crop.CropImageActivity" />
18+
19+
<provider android:authorities="com.soundcloud.android.crop.example.capture"
20+
android:enabled="true" android:exported="true" android:name=".SharePhotoProvider"/>
21+
1822
</application>
1923

2024
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2125

26+
<uses-permission android:name="android.permission.CAMERA" />
27+
2228
</manifest>

example/src/main/java/com/soundcloud/android/crop/example/MainActivity.java

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Intent;
77
import android.net.Uri;
88
import android.os.Bundle;
9+
import android.provider.MediaStore;
910
import android.view.Menu;
1011
import android.view.MenuItem;
1112
import android.view.View;
@@ -18,6 +19,7 @@ public class MainActivity extends Activity {
1819

1920
private ImageView resultView;
2021
private View progress;
22+
private int REQUEST_SHOOT = 5015;
2123

2224
@Override
2325
protected void onCreate(Bundle savedInstanceState) {
@@ -41,20 +43,37 @@ public boolean onOptionsItemSelected(MenuItem item) {
4143
resultView.setImageDrawable(null);
4244
Crop.pickImage(this);
4345
return true;
46+
} else if (item.getItemId() == R.id.action_camera) {
47+
showProgress();
48+
resultView.setImageDrawable(null);
49+
shoot();
50+
return true;
4451
}
4552
return super.onOptionsItemSelected(item);
4653
}
4754

55+
private void shoot() {
56+
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
57+
intent.putExtra(MediaStore.EXTRA_OUTPUT, getCaptureUri());
58+
startActivityForResult(intent, REQUEST_SHOOT);
59+
}
60+
4861
@Override
4962
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
5063
hideProgress();
5164
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
5265
beginCrop(result.getData());
66+
} else if (requestCode == REQUEST_SHOOT && resultCode == RESULT_OK) {
67+
beginCrop(getCaptureUri());
5368
} else if (requestCode == Crop.REQUEST_CROP) {
5469
handleCrop(resultCode, result);
5570
}
5671
}
5772

73+
private Uri getCaptureUri() {
74+
return Uri.parse("content://com.soundcloud.android.crop.example.capture/capture.jpg");
75+
}
76+
5877
private void beginCrop(Uri source) {
5978
showProgress();
6079
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.soundcloud.android.crop.example;
2+
3+
import android.content.ContentProvider;
4+
import android.content.ContentValues;
5+
import android.database.Cursor;
6+
import android.net.Uri;
7+
import android.os.ParcelFileDescriptor;
8+
import android.support.annotation.Nullable;
9+
10+
import java.io.File;
11+
import java.io.FileNotFoundException;
12+
import java.io.IOException;
13+
14+
public class SharePhotoProvider extends ContentProvider {
15+
16+
@Override
17+
public boolean onCreate() {
18+
try {
19+
new File(getContext().getCacheDir(), "capture.jpg").createNewFile();
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
return true;
24+
}
25+
26+
@Nullable
27+
@Override
28+
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
29+
return ParcelFileDescriptor.open(new File(getContext().getCacheDir(), "capture.jpg"),
30+
ParcelFileDescriptor.MODE_READ_WRITE);
31+
}
32+
33+
@Nullable
34+
@Override
35+
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
36+
return null;
37+
}
38+
39+
@Nullable
40+
@Override
41+
public String getType(Uri uri) {
42+
return null;
43+
}
44+
45+
@Nullable
46+
@Override
47+
public Uri insert(Uri uri, ContentValues values) {
48+
return null;
49+
}
50+
51+
@Override
52+
public int delete(Uri uri, String selection, String[] selectionArgs) {
53+
return 0;
54+
}
55+
56+
@Override
57+
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
58+
return 0;
59+
}
60+
}

example/src/main/res/menu/activity_main.xml

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
android:title="@string/action_select"
77
android:showAsAction="always" />
88

9+
<item android:id="@+id/action_camera"
10+
android:title="@string/action_camera"
11+
android:showAsAction="always" />
12+
913
</menu>

example/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
<string name="app_name">Crop Demo</string>
44
<string name="action_select">Pick</string>
5+
<string name="action_camera">Shoot</string>
56

67
</resources>

0 commit comments

Comments
 (0)