gpt4 book ai didi

android - Nexus 相机——从不返回数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:21 24 4
gpt4 key购买 nike

在应用程序中,我允许用户将他们的头像图片更改为他们选择的图片之一。图片(裁剪后)随后存储在应用程序的私有(private)上下文中。我所做的事情取得了巨大的成功,但是,在 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;
}
}

最佳答案

这是一个常见问题,一些型号的设备使用不同的额外关键属性,

所以命令为
位图照片 =(Bitmap) data.getExtras().get("data");

可以指向空元素或小缩略图元素

查看this article

关于android - Nexus 相机——从不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11436535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com