在应用程序中,我允许用户将他们的头像图片更改为他们选择的一种。然后,图片(一次裁剪)存储在应用程序的私有上下文中。我在做什么方面取得了巨大的成功,但是,在Nexus上,相机从不返回数据,因此方法可以继续前进。它只是坐着等待,直到你必须手动强制关闭应用程序。它正在研究其他4.0 ICS设备,但不在Nexus上。Nexus允许用户从他们的图库中选择,它的工作原理很好,而不是在拍摄新照片时。有什么窍门让这个工作?Nexus相机 - 永不返回数据
这里是代码段:
再次请注意,这是工作在其他设备上不会有问题:
最终的String []项目=新的String [] {“从相机拍摄“,”从画廊中选择“};
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;
}
}
谢谢你这么CH!我相信这将解决我用这款手机所遇到的问题。我仍然没有确定为什么会出现问题,但这是一个很好的学习经历。 – TDev 2012-07-12 14:49:29