android从相册中选择报无法加载照片

最近学点android,代码都是来自第一行代码这本书。按书上代码实现后,报无法加载。

有时候,如果图片太大,也可能出现加载错误。所以加一个剪切功能还是不错的

正确实现如下:

chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
chooseFromAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File outputImage = new File(Environment
.getExternalStorageDirectory(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.intent.action.PICK");
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);//传错了参数
}
});



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {

//书中代码这没有赋值
if(data != null) {
imageUri = data.getData();
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
imageUri));
picture.setImageBitmap(bitmap); 
} catch (Exception e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}

你可能感兴趣的:(android)