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

Add camera shoot action on Example app #170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -15,8 +15,14 @@
</intent-filter>
</activity>
<activity android:name="com.soundcloud.android.crop.CropImageActivity" />

<provider android:authorities="com.soundcloud.android.crop.example.capture"
android:enabled="true" android:exported="true" android:name=".SharePhotoProvider"/>

</application>

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

<uses-permission android:name="android.permission.CAMERA" />

</manifest>
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

@@ -16,12 +18,16 @@
public class MainActivity extends Activity {

private ImageView resultView;
private View progress;
private int REQUEST_SHOOT = 5015;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultView = (ImageView) findViewById(R.id.result_image);
progress = findViewById(R.id.progress);
hideProgress();
}

@Override
@@ -33,33 +39,61 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_select) {
showProgress();
resultView.setImageDrawable(null);
Crop.pickImage(this);
return true;
} else if (item.getItemId() == R.id.action_camera) {
showProgress();
resultView.setImageDrawable(null);
shoot();
return true;
}
return super.onOptionsItemSelected(item);
}

private void shoot() {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getCaptureUri());
startActivityForResult(intent, REQUEST_SHOOT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
hideProgress();
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
beginCrop(result.getData());
} else if (requestCode == REQUEST_SHOOT && resultCode == RESULT_OK) {
beginCrop(getCaptureUri());
} else if (requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, result);
}
}

private Uri getCaptureUri() {
return Uri.parse("content://com.soundcloud.android.crop.example.capture/capture.jpg");
}

private void beginCrop(Uri source) {
showProgress();
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).asSquare().start(this);
}

private void handleCrop(int resultCode, Intent result) {
hideProgress();
if (resultCode == RESULT_OK) {
resultView.setImageURI(Crop.getOutput(result));
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}

private void hideProgress() {
progress.setVisibility(View.INVISIBLE);
}

private void showProgress() {
progress.setVisibility(View.VISIBLE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.soundcloud.android.crop.example;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.support.annotation.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SharePhotoProvider extends ContentProvider {

@Override
public boolean onCreate() {
try {
new File(getContext().getCacheDir(), "capture.jpg").createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

@Nullable
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
return ParcelFileDescriptor.open(new File(getContext().getCacheDir(), "capture.jpg"),
ParcelFileDescriptor.MODE_READ_WRITE);
}

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}

@Nullable
@Override
public String getType(Uri uri) {
return null;
}

@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
9 changes: 7 additions & 2 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -8,7 +8,12 @@
android:id="@+id/result_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:background="@drawable/texture" />
android:scaleType="fitCenter" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

</FrameLayout>
4 changes: 4 additions & 0 deletions example/src/main/res/menu/activity_main.xml
Original file line number Diff line number Diff line change
@@ -6,4 +6,8 @@
android:title="@string/action_select"
android:showAsAction="always" />

<item android:id="@+id/action_camera"
android:title="@string/action_camera"
android:showAsAction="always" />

</menu>
1 change: 1 addition & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -2,5 +2,6 @@

<string name="app_name">Crop Demo</string>
<string name="action_select">Pick</string>
<string name="action_camera">Shoot</string>

</resources>