1 回答
TA贡献1831条经验 获得超4个赞
使用 startActivityForResult 使您的按钮在此处调用活动工具: https ://www.youtube.com/watch?v=w06OnGwhh4I&feature=youtu.be
此活动从相机、画廊、谷歌驱动器等中挑选图像。裁剪图像并将其旋转到。代码Java:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.android.controledevendas.R;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
public class Image_Cropper_Activity extends AppCompatActivity {
private ImageButton btn_browse;
private ImageButton btn_reset;
private ImageView imageView;
private Uri uriPass = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image__cropper_);
btn_browse = findViewById(R.id.image_cropper_btn_add_picture);
btn_reset = findViewById(R.id.image_cropper_btn_reset);
imageView = findViewById(R.id.cropper_image);
btn_browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CropImage.startPickImageActivity(Image_Cropper_Activity.this);
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageBitmap(null);
uriPass = null;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_cliente_fornecedor, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (android.R.id.home):
setResult(RESULT_OK);
finish();
return true;
case (R.id.save_client_fornecedor):
saveImage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Uri imageUri = CropImage.getPickImageResultUri(this, data);
if(CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)){
}
else {
startCrop(imageUri);
}
}
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK){
imageView.setImageURI(result.getUri());
uriPass = result.getUri();
//Toast.makeText(this, "Imagem Atualizada Com Sucesso!", Toast.LENGTH_SHORT).show();
}
}
}
private void startCrop(Uri imageUri) {
CropImage.activity(imageUri).setGuidelines(CropImageView.Guidelines.ON).setMultiTouchEnabled(true).start(this);
}
private void saveImage() {
if (uriPass==null){
Toast.makeText(this, "Por favor, insira uma imagem.", Toast.LENGTH_SHORT).show();
return;
}
Intent dataForAddEditScreen = new Intent();
dataForAddEditScreen.setData(uriPass);
setResult(RESULT_OK, dataForAddEditScreen);
finish();
}
}
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
tools:context=".Activities.Image_Cropper_Activity">
<ImageView
android:id="@+id/cropper_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="20dp">
<ImageButton
android:id="@+id/image_cropper_btn_add_picture"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/bg_round"
app:srcCompat="@drawable/ic_image_sample_50x50_white" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/image_cropper_btn_reset"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/bg_round"
app:srcCompat="@drawable/ic_refresh_50x50_white" />
</LinearLayout>
</LinearLayout>
返回图片 Uri 后:
private ImageView imageProduto;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__edit__produto_);
imageProduto = findViewById(R.id.image_produto_detail);
}
...
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Constantes.REQUEST_CODE_ADD_PICTURE && resultCode == RESULT_OK && data!= null){
Uri imageUri = data.getData();
imageProduto.setImageURI(imageUri);
Toast.makeText(this, "Image Update Successfully!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Image Not Updated!", Toast.LENGTH_SHORT).show();
}
}
添加回答
举报