无法在 Android 中使用 Android Crop 图像裁剪库
Posted
技术标签:
【中文标题】无法在 Android 中使用 Android Crop 图像裁剪库【英文标题】:Cannot use Android Crop image crop library in Android 【发布时间】:2016-06-11 06:21:26 【问题描述】:我正在开发一个 android 项目。在我的项目中,我想添加裁剪图像功能。所以我使用了这个库,https://github.com/jdamcd/android-crop。但是当我使用它时。它给了我错误。
我是这样安装的:
我把这个放在grandle中,编译'com.soundcloud.android:android-crop:1.0.1@aar'
我把它放在清单文件中,
这就是我在代码中的使用方式:
我像这样打开图像选择器
Crop.pickImage(CurrentActivity.this, IMAGE_CHOOSER_REQUEST_COODE);
在onActivityResult中,我是这样做的
@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();
但是在我裁剪图像并单击“确定”后,活动的结果不等于确定。所以我无法检索裁剪的图像数据。
所以我删除了resultCode==RESULT_OK
,然后我这样检索:
if(requestCode == Crop.REQUEST_CROP)
Uri filePath = data.getData();
数据始终为空。我的代码有什么问题,我该如何解决?
【问题讨论】:
【参考方案1】:将此添加到build.gradle
文件中
repositories
mavenCentral()
dependencies
compile 'com.edmodo:cropper:1.0.1'
像这样布局Image
<?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_
android:layout_
tools:context=".MainActivity">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:padding="@dimen/content_padding">
<TextView
android:id="@+id/title"
android:layout_
android:layout_
android:layout_gravity="center"
android:text="@string/title"
android:textSize="24sp"
android:textStyle="bold"/>
<com.edmodo.cropper.CropImageView
android:id="@+id/CropImageView"
android:layout_
android:layout_
android:layout_marginTop="@dimen/content_padding"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/butterfly"/>
<LinearLayout
android:id="@+id/fixedAspectRatioLayout"
android:layout_
android:layout_
android:layout_marginTop="@dimen/content_padding"
android:orientation="horizontal">
<TextView
android:id="@+id/fixedAspectRatio"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical|end"
android:text="@string/fixedAspectRatio"/>
<FrameLayout
android:layout_
android:layout_
android:layout_weight="1">
<ToggleButton
android:id="@+id/fixedAspectRatioToggle"
android:layout_
android:layout_
android:layout_gravity="center_vertical|start"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:layout_marginTop="@dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="@+id/aspectRatioXHeader"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="@string/aspectRatioXHeader"/>
<TextView
android:id="@+id/aspectRatioX"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="start"
tools:text="10"/>
</LinearLayout>
<SeekBar
android:id="@+id/aspectRatioXSeek"
android:layout_
android:layout_
android:max="100"
android:progress="10"/>
<LinearLayout
android:layout_
android:layout_
android:layout_marginTop="@dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="@+id/aspectRatioYHeader"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="@string/aspectRatioYHeader"/>
<TextView
android:id="@+id/aspectRatioY"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="start"
tools:text="10"/>
</LinearLayout>
<SeekBar
android:id="@+id/aspectRatioYSeek"
android:layout_
android:layout_
android:max="100"
android:progress="10"/>
<LinearLayout
android:id="@+id/showGuidelinesLayout"
android:layout_
android:layout_
android:layout_marginTop="@dimen/content_padding_half"
android:orientation="horizontal">
<TextView
android:id="@+id/showGuidelines"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="end"
android:text="@string/showGuidelines"/>
<FrameLayout
android:layout_
android:layout_
android:layout_weight="1">
<Spinner
android:id="@+id/showGuidelinesSpin"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:entries="@array/showGuidelinesArray"
android:gravity="start"/>
</FrameLayout>
</LinearLayout>
<Button
android:id="@+id/Button_crop"
android:layout_
android:layout_
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_
android:layout_
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);
);
更多详情请访问:https://github.com/edmodo/cropper
【讨论】:
@Wai Yan Hein 在下面看到我的回答。 是的。但是对于那个,我需要升级sdk、库等等。我的互联网连接速度很慢,并且在我当前的位置数据很昂贵。这就是我使用这个的原因。所以这个库不工作了? 是的。谢谢。那个图书馆很有用。那么您可以编辑提及该库的答案吗?所以我可以投票给答案。 @WaiYanHein 很高兴为您提供帮助。以上是关于无法在 Android 中使用 Android Crop 图像裁剪库的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio JNI:无法编译JPEG c库