1

I use intent with action android.provider.MediaStore.ACTION_IMAGE_CAPTURE to capture image and get a path of the the image. After got the path, I decode it to a bitmap.

Bitmap photoBitmap = BitmapFactory.decodeFile(currentPhotoPath);

All work well on Device1, the log for image path look like this :

/storage/emulated/0/Pictures/QPOS/IMG_20160622_110127_1473532607.jpg

Then I run the app to a new device call Device2. The image path I got after capture image:

/storage/emulated/0/Pictures/QPOS/IMG_20160622_105436_2068195440.jpg

It look normal. But the decode method return a null bitmap at this line

Bitmap photoBitmap = BitmapFactory.decodeFile(currentPhotoPath);

I do not know why have different between two device, I build on the same source code. Can you explain why I got null bitmap for method decode file ? And how to fix this issue ? Thanks.

Update: I add request permission for API 23 by this method:

private void requestPermissions() {
        Log.d("binh", "request Permision");
        if (Build.VERSION.SDK_INT >= 23) {
            List<String> permissionRequestList = new ArrayList<>();

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionRequestList.add(android.Manifest.permission.CAMERA);
            }

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionRequestList.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
            }

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionRequestList.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
            }

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionRequestList.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionRequestList.add(android.Manifest.permission.READ_PHONE_STATE);
            }

            if (permissionRequestList.size() > 0) {
                ActivityCompat.requestPermissions(this, permissionRequestList.toArray(new String[permissionRequestList.size()]), Constants.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            }
        }
    }

This is a code to create path for the image:

private File setUpPhotoFile() throws IOException {
        File f = createImageFile();
        currentPhotoPath = f.getAbsolutePath();
        return f;
    }

    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
        File albumF = getAlbumDir();
        File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
        return imageF;
    }

    private File getAlbumDir() {
        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
            storageDir = new File(
                    getExternalFilesDir(Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
            if (storageDir != null) {
                if (!storageDir.mkdirs()) {
                    if (!storageDir.exists()) {
                        Log.d("CameraSample", "failed to create directory");
                        return null;
                    }
                }
            }
        } else {
            Log.d(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
        }


        return storageDir;
    }

After build and granted permission from dialog, I got the same issue.

CauCuKien
  • 1,027
  • 2
  • 15
  • 26
  • Did you manage to find a solution? I read through different topics on SOF and you have exactly the same than me. Every other topic has "similar issue" but at the end their solution does not apply for our issue, not sure why it was flagged as duplicate. Or maybe I missed the good one? – Laurent Aug 20 '16 at 14:59

2 Answers2

3

May be your Device1 's Android sdk < 23 and Device2 's Android sdk == 23 please check that READ_EXTERNAL_STORAGE permission is managed according to Android Sdk 23(at run time required) as well.

Ashish
  • 997
  • 6
  • 20
  • maybe it is the reason, let me check and feed back soon – CauCuKien Jun 22 '16 at 04:58
  • private boolean mayRequestPermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return true; } else { if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { return true; } else { requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 0); return false; } } }//if u r in AppcompactActivity try this before create bitmap – Ashish Jun 22 '16 at 08:04
  • I updated the question, please have a look on it. Thanks – CauCuKien Jun 22 '16 at 08:17
0

It seems like the issue of alpha channel, so you should use a format for bitmap that preserves alpha:

check below code:

  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  Bitmap photoBitmap = BitmapFactory.decodeFile(currentPhotoPath, options);
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34