我看了别人的播客和自己写的demo发现一个问题,那就是图片裁剪的时候,如果你输入的裁剪分辨率较高的话,那么你就不能用bitmap方式来保存,这是谷歌那边定义的,因为图片太大就是把app的内存占满了。所以最好是你传入的是uri,直接在裁剪的时候设置output到指定的uri中,然后再onActivityResult哪里去设置图片。但是如果你的分辨率设置的很高的话又想在onActivityResult哪里拿到bitmap的话就会卡主在裁剪界面。所以最好是完全对uri进行操作比较好。此外相机拍摄的图片,在OnActivityResult哪里的data是空的。
package com.example.go2zoompic;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageView;
private File afterZoomFile;
private File picFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.iv);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new ZoomPic(MainActivity.this, imageView);
}
});
}
public void go2AlbumAndZoom(View view) {
goImageAlbum();
}
public void go2CameraAndZoom(View view) {
goCamera();
}
private void goCamera() {
Intent captrueIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
picFile = getUri();
if (picFile == null) {
return;
}
captrueIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
startActivityForResult(captrueIntent, 3);
}
private File getUri() {
File dcimFile = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (!dcimFile.exists()) {
Toast.makeText(MainActivity.this, "使用相机前请插入sd卡!",
Toast.LENGTH_SHORT).show();
return null;
}
File file = new File(dcimFile.getAbsolutePath() + "/dcim"
+ System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = null;
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (data != null) {
uri = data.getData();
goZoomImage(uri, 1000, 1000);
} else {
Toast.makeText(MainActivity.this, "请选择图片",
Toast.LENGTH_SHORT).show();
}
}
if (requestCode == 2) {
if (data == null) {
Toast.makeText(MainActivity.this, "图片不存在了,亲",
Toast.LENGTH_SHORT).show();
return;
}
imageView.setImageURI(Uri.fromFile(afterZoomFile));
}
if (requestCode == 3) {
// 从相机拍摄的图片,返回的data是空的
goZoomImage(Uri.fromFile(picFile), 1000, 1000);
}
}
}
/**
* 跳转到图片裁剪
*
* @param uri
*/
public void goZoomImage(Uri uri, int width, int height) {
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(afterZoomFile = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM)
.getAbsolutePath()
+ "/dcim" + System.currentTimeMillis() + ".jpg")));
// 设置裁剪后的分辨率
intent.putExtra("outputX", width);
intent.putExtra("outputY", height);
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", width);
intent.putExtra("aspectY", height);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "请先安装图库", Toast.LENGTH_SHORT)
.show();
}
}
/**
* 跳转到图库
*/
public void goImageAlbum() {
try {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// 方法2
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*"); // 设置文件类型
startActivityForResult(intent, 1);// 转到图片
} catch (Exception e) {
Toast.makeText(MainActivity.this, "请先安装图库", Toast.LENGTH_SHORT)
.show();
}
}
}