捕获图像和文件从库中选择后如何添加裁剪活动?

时间:2017-03-25 17:35:44

标签: java android xml

这是我的document.java代码。当我捕捉图像显示的构象。当我得到确认时,图像显示在ImageView中。在构造之前,我想裁剪图像。而且我还希望从图库中选择图像选项(从内部存储导入)。

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import java.io.IOException;
import static com.dk.deepan.salvador.R.id.img;
public class Document extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private int PICK_IMAGE_REQUEST = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_document);


    //first edittext
    EditText txtDate =(EditText)findViewById(R.id.issue);
    txtDate.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            DateDialog dialog=new DateDialog(v);
            FragmentTransaction ft =getFragmentManager().beginTransaction();
            dialog.show(ft, "DatePicker");
        }
    });

    //second edittext
    EditText txtDate2 =(EditText)findViewById(R.id.expiry);
    txtDate2.setOnClickListener (new View.OnClickListener(){
           @Override
            public void onClick(View v) {
                DateDialog dialog=new DateDialog(v);
                FragmentTransaction ft =getFragmentManager().beginTransaction();
                dialog.show(ft, "DatePicker");
            }

    });


    //take a photo
    this.imageView = (ImageView)this.findViewById(img);
    Button photoButton = (Button) this.findViewById(R.id.camera);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
    //Attach file

    Button imageView = (Button) findViewById(R.id.attachfile);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            // Show only images, no videos or anything else
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            // Always show the chooser (if there are multiple options available)
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
        }
    });



    //spinner for select document
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.document_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    //take photo
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(R.id.img);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
  }
 }
}

1 个答案:

答案 0 :(得分:0)

https://github.com/jdamcd/android-crop 请按照以上网址查看作物