0

So I created camera app that works with camera intent, but for some reason on some phones it doesn't display pictures when they are captured. Later I found out that it mostly won't work with cameras higher then 8mpx. Here is my code that captures image:

    Intent cameraIntent = newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File output = new File(dir,"pic.png");
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
    path = output.getAbsolutePath();
    startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO);

And displaying them:

           @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CODE_PHOTO) {

            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;

            int targetW = width;
            int targetH = height;

            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, bmOptions);
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;

            Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);
            iv.setImageBitmap(bitmap);
 }

The problem is that it doesn't work with more than 8mpx camera phones like lg g2 and samsung note 3, but for some reason works on my zenfone 2 ze551ml. So I have no clue what can cause this issue maybe anyone have experience with this. Thanks in advance.

Ktokto
  • 11
  • 5
  • define `doesn't work` – njzk2 Nov 30 '15 at 16:21
  • @njzk2 it doesn't crash, but it doesn't show picture either just blank screen. – Ktokto Nov 30 '15 at 16:24
  • there are various things you can verify first: is `scaleFactor` 0? (because integer division), is `bitmap` null? if not, does it have a size that is what you expect? does `BitmapFactory.decodeFile(path, bmOptions);` really work? is there an image at `path`? – njzk2 Nov 30 '15 at 16:29
  • 1
    Bear in mind that there are buggy camera apps out there, ones that will ignore `EXTRA_OUTPUT`. Also, to work with your designated directory, you need to have `READ_EXTERNAL_STORAGE` as a permission (and, if your `targetSdkVersion` is 23 or higher, have the appropriate code for dealing with runtime permissions). `path` also needs to be saved in your saved instance state `Bundle`, as your process may be terminated while the camera app is open. – CommonsWare Nov 30 '15 at 16:53
  • I believe this is yet another manifestation of [Android startCamera gives me null Intent and ... does it destroy my global variable?](http://stackoverflow.com/questions/20424909). TL;NR: save and restore the Activity instance. – Alex Cohn Nov 30 '15 at 17:33

0 Answers0