2

I thought it was simple to capture camera image to a file, since there are many examples. But after tying a lot of them, I still not get it work.

My code is:

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");
            }
        }
    }
}

But it still get "Can't get bitmap from camera" shown. I don't known where is wrong.

I also created a working demo: https://github.com/freewind/AndroidCameraTest, you can see the full code there, and you may clone it and have a try on your own android device :)


Update

This code is working fine on android emulators, but not on my android pad.

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • 1
    Have you added this - `` in your `AndroidManifest.xml` And, have a look at [this](http://stackoverflow.com/a/5991757/940096) and also [this](http://stackoverflow.com/a/5661567/940096) may useful too. – Praveenkumar Sep 27 '12 at 04:32
  • I have these features and camera permissions, but still get the same result. I have read the 2 links, but now helpful. – Freewind Sep 27 '12 at 04:55
  • Sorry, there is a typo in my last comment, it should be: `but not helpful` -_- – Freewind Sep 27 '12 at 05:18

1 Answers1

0

I have seen this problem too; I removed intent.putExtra(MediaStore.EXTRA_OUTPUT,...), and it worked using the method:

stream = getContentResolver().openInputStream(data.getData());
image = BitmapFactory.decodeStream(stream);
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127