1

Hi I am trying to make the images captured from my app inaccessible to the user. First I tried to save these images to internal storage which didnt work. Then I tried to hide them using "." infront of the folder name.I am not sure what the correct way to do this is. I also tried creating a file called .nomedia to bypass media scanner. I am very confused about the proper way to do this. Here's my code:

  public String getImageUri(Context inContext, Bitmap inImage) {
    /* *//*  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, ".title", null);
    return Uri.parse(path);*//*
     */

    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "/.myFolder");
    file.mkdirs();
    File mFile = new File(Environment.getExternalStorageDirectory()
            + File.separator + "/.nomedia");
     mFile.mkdirs();
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(file);
        inImage.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();
       uri =   MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return uri;

}

If I use file.mkdirs() I get filenotfoundexception. If i remove that line I get no errors but my uri is empty.

Does the above function return the file path as well? I need the file path and the uri later on. Any help is appreciated.

sandy
  • 85
  • 8

4 Answers4

1

I guess you don't have to add another extension or something else just save them in external cache dir of your app and gallery app won't able to read your private directory until unless you notify about them.

so store your camera images here and no gallery app can detect it.

context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

sample code

 public static File createPictureFile(Context context) throws IOException {
        Locale locale = Locale.getDefault();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", locale).format(new Date());
        String fileName = "JPEG_" + timeStamp + "_";
        // Store in normal camera directory
        File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(fileName, ".jpg", storageDir);
    }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • 1
    you can also refer https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android – Jitesh Mohite Apr 11 '18 at 06:51
0

Save your image to internal storage instead. Other applications like MediaScanner or Gallery do not have permission to read from your own application memory. Sample code:

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
0

Save the image with different extension.

For example: .jpg can be saved as .ttj.

Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
0

Try this code. Set where you want to save the photo. Once you receive response on onActivityResult(), in the desired URI, you will get the photo.

public void captureImageFromCamera() {

        fileUri = FileUtils.getInstance().getOutputMediaFile(null);
        if(fileUri == null){
            Utilities.displayToastMessage(ApplicationNekt.getContext(),
                    context.getString(R.string.unable_to_access_image));
            return;
        }

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, TAKE_PICTURE);
    }

Following function will give you desired path. Change it based on your project need.

public Uri getOutputMediaFile(String imageNameWithExtension) {

        if (imageNameWithExtension == null)
            imageNameWithExtension = "image_" + System.currentTimeMillis() + ".jpg";

        String extPicDir = getExtDirPicturesPath();
        if (!Utilities.isNullOrEmpty(extPicDir)) {
            File mediaFile = new File(extPicDir, imageNameWithExtension);
            return Uri.fromFile(mediaFile);
        } else {
            Log.d("tag", "getOutputMediaFile." +
                    "Empty external path received.");
            return null;
        }

    }

public String getExtDirPicturesPath() {
        File file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (file != null)
            return file.getPath();
        else
            Log.d("", "getExtDirPicturesPath failed. Storage state: "
                    + Environment.getExternalStorageState());
        return null;
    }

To get the resultant photo.

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

        if (resultCode != Activity.RESULT_OK)
            return;

        switch (requestCode) {
            case TAKE_PICTURE:
                //fileuri variable has the path to your image.
                break;
         }
Aram
  • 952
  • 1
  • 8
  • 30