2

我认为将相机图像捕获到文件很简单,因为有很多示例。但是在绑定了很多之后,我仍然无法正常工作。

我的代码是:

public class MyActivity extends Activity {

    private Button btn;
    private ImageView imageView;

    private static final File photoPath = new File(Environment.getExternalStorageState(), "camera.jpg");

    private static final int CAMERA = 1;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViews();
        setListeners();
    }

    private void setListeners() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoPath));
                startActivityForResult(intent, CAMERA);
            }
        });
    }

    private void findViews() {
        btn = (Button) findViewById(R.id.btn);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA) {
            if (resultCode == RESULT_OK) {
                try {
                    Bitmap bitmap = getCameraBitmap(data);
                    if (bitmap == null) {
                        Toast.makeText(MyActivity.this, "Can't get bitmap from camera", Toast.LENGTH_LONG).show();
                    } else {
                        imageView.setImageBitmap(bitmap);
                    }
                } catch (IOException e) {
                    Toast.makeText(MyActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    public Bitmap getCameraBitmap(Intent data) throws IOException {
        if (data == null) {
            // try solution 1
            try {
                return MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(photoPath));
            } catch (FileNotFoundException e) {
                return BitmapFactory.decodeFile(photoPath.getAbsolutePath());
            }
        } else {
            Uri image = data.getData();
            if (image != null) {
                // try solution 3
                InputStream inputStream = getContentResolver().openInputStream(image);
                return BitmapFactory.decodeStream(inputStream);
            } else {
                // try solution 4
                return (Bitmap) data.getExtras().get("data");
            }
        }
    }
}

但它仍然显示“无法从相机获取位图”。我不知道哪里错了。

我还创建了一个工作演示:https ://github.com/freewind/AndroidCameraTest ,你可以在那里看到完整的代码,你可以克隆它并在你自己的安卓设备上尝试:)


更新

这段代码在 android 模拟器上运行良好,但在我的 android pad 上却不行。

4

1 回答 1

0

我也看到过这个问题;我删除intent.putExtra(MediaStore.EXTRA_OUTPUT,...)了,它使用以下方法工作:

stream = getContentResolver().openInputStream(data.getData());
image = BitmapFactory.decodeStream(stream);
于 2013-04-11T07:04:57.093 回答