android


Cannot use Android Crop image crop library in Android


I am developing an Android project. In my project, I want to add crop image feature. So I used this library, https://github.com/jdamcd/android-crop. But when I use it. It is giving me error. Please see my scenario below.
This is how I installed.
I put this in grandle, compile 'com.soundcloud.android:android-crop:1.0.1#aar'
I put this in manifest file,
This is how I am using in code.
I open Image picker like this
Crop.pickImage(CurrentActivity.this, IMAGE_CHOOSER_REQUEST_COODE);
In onActivityResult, I am doing like this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==IMAGE_CHOOSER_REQUEST_COODE && resultCode == RESULT_OK)
{
Crop.of(data.getData(),null).asSquare().start(CreateItemActivity.this);
}
else if(requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK)
{
Uri filePath = data.getData();
}
}
But after I crop the image and I click "OK", the result of the activity is not equal to OK. So I cannot retrieve cropped image data.
So I removed resultCode==RESULT_OK and I retrieved like this
if(requestCode == Crop.REQUEST_CROP)
{
Uri filePath = data.getData();
}
Data is always null. So please suggest what is wrong with my code and how can I fix it? Please help me. What is the correct way to use it.
Add this in build.gradle file
repositories {
mavenCentral()
}
dependencies {
compile 'com.edmodo:cropper:1.0.1'
}
Make the layout of Image like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/scrollview"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/content_padding">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/title"
android:textSize="24sp"
android:textStyle="bold"/>
<com.edmodo.cropper.CropImageView
android:id="#+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/butterfly"/>
<LinearLayout
android:id="#+id/fixedAspectRatioLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding"
android:orientation="horizontal">
<TextView
android:id="#+id/fixedAspectRatio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical|end"
android:text="#string/fixedAspectRatio"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ToggleButton
android:id="#+id/fixedAspectRatioToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="#+id/aspectRatioXHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="#string/aspectRatioXHeader"/>
<TextView
android:id="#+id/aspectRatioX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="start"
tools:text="10"/>
</LinearLayout>
<SeekBar
android:id="#+id/aspectRatioXSeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="#+id/aspectRatioYHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="#string/aspectRatioYHeader"/>
<TextView
android:id="#+id/aspectRatioY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="start"
tools:text="10"/>
</LinearLayout>
<SeekBar
android:id="#+id/aspectRatioYSeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"/>
<LinearLayout
android:id="#+id/showGuidelinesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="#+id/showGuidelines"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="#string/showGuidelines"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Spinner
android:id="#+id/showGuidelinesSpin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:entries="#array/showGuidelinesArray"
android:gravity="start"/>
</FrameLayout>
</LinearLayout>
<Button
android:id="#+id/Button_crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/content_padding"
android:minWidth="120dp"
android:text="#string/crop"
android:textColor="#33B5E5"
android:textSize="20sp"/>
<ImageView
android:id="#+id/croppedImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/content_padding"
android:adjustViewBounds="true"
android:contentDescription="#string/croppedImageDesc"
android:scaleType="centerInside"/>
</LinearLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends Activity {
private static final int GUIDELINES_ON_TOUCH = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// Initialize Views.
final ToggleButton fixedAspectRatioToggleButton = (ToggleButton) findViewById(R.id.fixedAspectRatioToggle);
final TextView aspectRatioXTextView = (TextView) findViewById(R.id.aspectRatioX);
final SeekBar aspectRatioXSeekBar = (SeekBar) findViewById(R.id.aspectRatioXSeek);
final TextView aspectRatioYTextView = (TextView) findViewById(R.id.aspectRatioY);
final SeekBar aspectRatioYSeekBar = (SeekBar) findViewById(R.id.aspectRatioYSeek);
final Spinner guidelinesSpinner = (Spinner) findViewById(R.id.showGuidelinesSpin);
final CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
final ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
final Button cropButton = (Button) findViewById(R.id.Button_crop);
// Initializes fixedAspectRatio toggle button.
fixedAspectRatioToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
cropImageView.setFixedAspectRatio(isChecked);
cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
aspectRatioXSeekBar.setEnabled(isChecked);
aspectRatioYSeekBar.setEnabled(isChecked);
}
});
// Set seek bars to be disabled until toggle button is checked.
aspectRatioXSeekBar.setEnabled(false);
aspectRatioYSeekBar.setEnabled(false);
aspectRatioXTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
aspectRatioYTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
// Initialize aspect ratio X SeekBar.
aspectRatioXSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar aspectRatioXSeekBar, int progress, boolean fromUser) {
if (progress < 1) {
aspectRatioXSeekBar.setProgress(1);
}
cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
aspectRatioXTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing.
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do nothing.
}
});
// Initialize aspect ratio Y SeekBar.
aspectRatioYSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar aspectRatioYSeekBar, int progress, boolean fromUser) {
if (progress < 1) {
aspectRatioYSeekBar.setProgress(1);
}
cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
aspectRatioYTextView.setText(String.valueOf(aspectRatioYSeekBar.getProgress()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing.
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do nothing.
}
});
// Set up the Guidelines Spinner.
guidelinesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cropImageView.setGuidelines(i);
}
public void onNothingSelected(AdapterView<?> adapterView) {
// Do nothing.
}
});
guidelinesSpinner.setSelection(GUIDELINES_ON_TOUCH);
// Initialize the Crop button.
cropButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Bitmap croppedImage = cropImageView.getCroppedImage();
croppedImageView.setImageBitmap(croppedImage);
}
});
}
}
for more detail visit this : https://github.com/edmodo/cropper

Related Links

facebook - recover from CLOSE_LOGIN_FAILED when user hits cancel button
Android: findViewById function in different class and different package
Implementing viewpager
Use Activity functions in no activity class
Process: com.android.packageinstaller OutOfMemoryError
issue on check boxes inside listview's every child item in android
How to implement checkbx with custom listview
4.2.2 - 4.4.2 Service Error
Jodatime issues on Android 5.0
Android Watch Face Development
compile android-support:support-annotations in java module. is it possible?
Seeing with sound software
Using HashMap to load image for specific marker crashes
Intent for GPS-location in NFC tag
Android: Attach dwelling time to Motion Events
Stop a method till condition is true

Categories

HOME
android
facebook-oauth
awk
mstest
apiblueprint
static-site
yandex
database-normalization
rds
babeljs
x264
elasticsearch-5
rcloud
openstreetmap
commonjs
heap
distribution
android-securityexception
datastage
favicon
libusb
screen-readers
azure-iot-hub
entitymanager
aws-sdk
nodemcu
oledb
database-connection
ag
undertow
gmock
pylons
ms-access-web-app
intersystems-cache
angular-translate
exchangewebservices
bringtofront
paperjs
bioinformatics
browser-sync
send
cart
reactjs.net
carriage-return
yowsup
servicestack-text
sim-toolkit
docfx
salesforce-communities
modernizr
apns-php
postgresql-8.4
android-exoplayer
lampp
java-6
oracleclient
truezip
excel-dna
boo
quicktime
grails-plugin
builtins
redistributable
beamer
android-dateutils
gcloud-ruby
turbo-prolog
blitline
highland.js
inf
lazybones
page.js
ember-cli-rails
ansi
broadband
entry-point
linkedin-jsapi
nsmenu
with-statement
radiant
android-framework
terminate
websphere-esb
koala
cffile
observium
twisted.web
alice
nokia-s40
rmiregistry
josql
wiimote
entitydatasource
cgbitmapcontextcreate
umra
timthumb
uideviceorientation
drupal-fivestar
glui
fail-fast-fail-early
fdf
flvplayback
httpcookie
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App