0

I have got trouble when take a picture using the Samsung Galaxy s6, and get the bitmap from that. It works correctly on other platform like Galaxy Note and s5, LG G@, and a Blackberry PRIV. But now, I have got a trouble when I work on Samsung Galaxy s6. Please give me solution who has got some experiences on that.

My code is below...

    String mCurrentPhotoPath;

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File albumF = getAlbumDir();
        if (albumF == null){
            return null;
        }
        File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
        mCurrentPhotoPath = "file:" + imageF.getAbsolutePath();
        return imageF;
    }

    private File getAlbumDir() {
        File storageDir = null;

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

            storageDir = new File(
                    Environment.getExternalStorageDirectory()
                            + "/dcim/"
                            + "test"
            );

            if (storageDir != null) {
                if (!storageDir.mkdirs()) {
                    if (!storageDir.exists()) {
                        Log.d("test", "failed to create directory");
                        CommonFunc.ShowAlertDialog(this,"test", "failed to create directory",null);
                        return null;
                    }
                }
            }

        } else {
            Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
            CommonFunc.ShowAlertDialog(this, getString(R.string.app_name), "External storage is not mounted READ/WRITE.", null);
        }

        return storageDir;
    }

    public void showUserImgCameraDialog(View v) {
        Intent takePicture = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        pFile = null;
        try {
            pFile = createImageFile();
        } catch (IOException e) {
            CommonFunc.ShowToast(this, "creatimagefile issue" + mCurrentPhotoPath);
        }
        if (pFile != null) {
            takePicture.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(pFile));
            try {
                startActivityForResult(takePicture,
                        100);
            } catch (Exception e) {
                CommonFunc.ShowAlertDialog(this, "test", "No Camera on Device:" + e.toString(), null);
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == RESULT_OK) {//upload user image from camera

            imageView = (ImageView) findViewById(R.id.ivAvatar);
            try {
                try {
                    FileInputStream in = new FileInputStream(pFile);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 10;
                    mCurrentPhotoPath = pFile.getAbsolutePath();
                    CommonFunc.ShowToast(this, mCurrentPhotoPath);
                    Bitmap _bmp = BitmapFactory.decodeStream(in, null, options);
                    imageView.setImageBitmap(_bmp);


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    CommonFunc.ShowAlertDialog(this, "test", "ImageFileNotFound", null);
                }
            }catch (Exception e){}
        }
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

I can't see the onActivity Camera Screen, and I think it is because the createImageFile path(mCurrentPhotoPath) is not correct on Samsung Galaxy S6. But this code works on other platforms except Samsung Galaxy S6.

If someone has got exp, please let me know. Thanks.

ps: I think it is because I am using the external storage on this code. But Samsung Galaxy S6 doesn't support the external storage like SD memory card. *****So I would like to know how to create temp file on internal storage of the Samsung Galaxy S6.*****

StarNeit
  • 23
  • 6
  • I think you issue is with the URI in Lollipop, check this link http://stackoverflow.com/questions/29646975/how-to-get-file-path-of-image-from-uri-in-android-lollipop – Miguel Benitez Apr 13 '16 at 12:58
  • What don't you see? Add log messages along the execution path, and paste the log to make it clear for people here. – Alex Cohn Apr 13 '16 at 14:11
  • Thanks for your reply. I think it is because I am using the external storage on this code. But Samsung Galaxy S6 doesn't support the external storage like SD memory card. So I would like to know how to create temp file on internal storage of the Samsung Galaxy S6. Thanks – StarNeit Apr 13 '16 at 15:23

1 Answers1

0

I know its 2 years passed but since @zaltan changed the inital question to

*****So I would like to know how to create temp file on internal storage of the Samsung Galaxy S6.*****

I would like to answer, its actually very simple:

mInternalCacheDir = context.getCacheDir();

Most probably you could create a temp file in this directory.
Or use the following method:

File createImageFile() throws IOException {
    String imageFileName = "JPEG_" + System.currentTimeMillis() + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
    return imageFile;
}

also please consider the following question - its about api 24 (Android N) and FileProvider approach. Samsung S6 most probably based on Android N and thus it might be your case.

Stan
  • 6,511
  • 8
  • 55
  • 87