在应用程序中,我允许用户将他们的头像图片更改为他们选择的一个。然后将图片(一旦裁剪)存储在应用程序的私有上下文中。我已经取得了很大的成功,但是,在Nexus上,相机永远不会返回数据,因此该方法可以继续前进。它只是坐着等待,直到你必须手动强制关闭应用程序。它正在开发其他4.0 ICS设备,但不在Nexus上.Nexus允许用户从他们的画廊中选择并且它工作正常,只是在拍摄新照片时。是否有一个技巧让这个工作?
以下是代码的一部分:
请再次注意,这可以在其他设备上运行而不会出现问题:
final String [] items = new String [] {“Take from camera”,“Select from gallery”};
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //take picture
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = builder.create();
mImageView = (ImageView) findViewById(R.id.me_photo);
File file = new File(context.getCacheDir(), "Avatar"+".png");
if (file.exists()) {
//Log.i("CACHE_test", file.getPath());
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
mImageView.setImageBitmap(bitmap);
}
mImageView.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
dialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
mImageCaptureUri= data.getData();
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo =(Bitmap) data.getExtras().get("data");
//extras.getParcelable("data");
mImageView.setImageBitmap(photo);
// FileOutputStream fos = null;
File file = new File(context.getCacheDir(), "Avatar"+".png");
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.PNG, 95, fos);
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "Sorry, Camera Crashed-Please Report as Crash A.", Toast.LENGTH_LONG).show();
}
}
break;
}
}
答案 0 :(得分:4)
这是一个常见问题, 某些型号的设备使用不同的额外密钥属性,
所以命令为
Bitmap photo =(Bitmap) data.getExtras().get("data");
可以指向null元素或小缩略图元素