android - 从系统相册或者拍照获得图片,剪裁后显示在imageview中
伊谢尔伦
伊谢尔伦 2017-04-17 11:55:45
0
0
369

出现的问题:
1.在选取照片进行裁剪后点击确定时,自动退出了程序。
2.在调用相机拍摄时,无法点击确定,就是无法进入裁剪界面。
我的代码如下:
`public class MainActivity extends Activity implements OnClickListener {

private Button camera;
private Button gallery;
private ImageView iv;
private File tempFile = new File(Environment.getDataDirectory(),
        getPhotoFileName());
/**
 * 调用相机
 */
private final int TOCAMERA = 1;
/**
 * 调用相册
 */
private final int TOGALLERY = 2;
/**
 * 调用裁剪
 */
private final int TOCROP = 3;

private static final String IMAGE_UNSPECIFIED = "image/*";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    camera = (Button) findViewById(R.id.camera);
    gallery = (Button) findViewById(R.id.gallery);
    iv = (ImageView) findViewById(R.id.iv);
    camera.setOnClickListener(this);
    gallery.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.camera:
        Intent intent0 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (!tempFile.exists()) {
            tempFile.getParentFile().mkdirs();
        }
        System.out.println(tempFile.getAbsolutePath());
        intent0.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        startActivityForResult(intent0, TOCAMERA);
        break;
    case R.id.gallery:
        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
        intent1.setDataAndType(
                MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                IMAGE_UNSPECIFIED);
        startActivityForResult(intent1, TOGALLERY);
        break;
    default:
        break;
    }
}

public void startPhotoZoom(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");// 调用Android系统自带的一个图片剪裁页面,
    intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
    intent.putExtra("crop", "true");// 进行修剪
    // aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // outputX outputY 是裁剪图片宽高
    intent.putExtra("outputX", 300);
    intent.putExtra("outputY", 500);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, TOCROP);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (data == null) {
        return;
    }
    switch (requestCode) {
    case TOGALLERY:
        startPhotoZoom(data.getData());
        break;
    case TOCROP:
        Uri originalUri = data.getData();
        if (originalUri == null) {
            return;
        }
        Bitmap bm = Utils.getBitpMap(this, originalUri);
        iv.setImageBitmap(bm);
        break;
    case TOCAMERA:
        // 设置文件保存的路径
        startPhotoZoom(Uri.fromFile(tempFile));
        break;
    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private String getPhotoFileName() {
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "'IMG'_yyyyMMdd_HHmmss");
    return dateFormat.format(date) + ".jpg";
}

}
`

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

membalas semua(0)
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan