在应用程序中,我允许用户将他们的头像图片更改为他们选择的一张。然后将图片(裁剪后)存储在应用程序的私有上下文中。我所做的事情取得了巨大的成功,但是,在 Nexus 上,相机永远不会返回数据,因此该方法可以继续前进。它只是坐下来等待,直到您必须手动强制关闭应用程序。它可以在其他 4.0 ICS 设备上运行,但不能在 Nexus 上运行。Nexus 允许用户从他们的图库中选择,它工作得很好,只是在拍摄新照片时不行。有什么诀窍可以让它工作吗???
下面是一段代码:
再次请注意,这可以在其他设备上正常工作:
final String [] items = new String [] {"从相机拍摄", "从图库中选择"};
ArrayAdapter 适配器 = 新的 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;
}
}