0

I need to make images invisible from my gallery and i see other answers on stackoverflow but did not work for me .

this how i done that programmatically .

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

if(resultCode== Activity.RESULT_OK){

    if(requestCode==REQUEST_CAMERA){

        Uri selectedImageUri = data.getData();

        if (null != selectedImageUri) {
            // Get the path from the Uri

            String path = getPathFromURI(selectedImageUri);
            File file = new File(path);
            Bitmap  bmp = CommonMethod.compressImage(file, getContext());
            Log.e(TAG, "onActivityResult --: "+ String.format("Size : %s", getReadableFileSize(file.length())));

            mCustomerImage =  CommonMethod.bitmapToByteArray(bmp);
            imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
            Log.e(TAG, "image: "+ imageTemplateStr );
            //CommonMethod.SaveImage(bmp);
            imageCustomer.setImageBitmap(bmp);

            CommonMethod.SaveImage(bmp);
        }

    }else if(requestCode==SELECT_FILE){
        Uri selectedImageUri = data.getData();
        if (null != selectedImageUri) {
            // Get the path from the Uri

            String path = getPathFromURI(selectedImageUri);
            File file = new File(path);
            Bitmap  bmp = CommonMethod.compressImage(file, getContext());
            Log.e(TAG, "onActivityResult --: "+ String.format("Size : %s", getReadableFileSize(file.length())));

            mCustomerImage =  CommonMethod.bitmapToByteArray(bmp);
            imageTemplateStr = Base64.encodeToString(mCustomerImage, Base64.DEFAULT);
            //CommonMethod.SaveImage(bmp);
            Log.e(TAG, "image: "+ imageTemplateStr );

            imageCustomer.setImageBitmap(bmp);

            CommonMethod.SaveImage(bmp);
        }

 }

SaveImage Method :

public static void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/safco_private_pics");
    if (!myDir.exists()) {
        myDir.mkdirs();
    }
    File newFile = new File(root,".nomedia");
    try {
        FileWriter writer = new FileWriter(newFile);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ())
        file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

getPathFromURI method

public String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

Now by that code images from gallery are not shown but images from Camera are visible in gallery and also .nomedia file not created.

Images are stored in my folder that i created and also in DCIM/Camera folder . I do not know why

What is wrong can anybody help me . ?

Rashid Kalhoro
  • 340
  • 4
  • 14

2 Answers2

0

let your image in hidden folder

//replace
File myDir = new File(root + "/safco_private_pics");
//with
File myDir = new File(root + "/.safco_private_pics");

put dot(.)before directory name it will create hidden directory and put your image in that directory

Mahendra Gohil
  • 380
  • 1
  • 3
  • 21
0

If you want to save the image for temporarily then you can use following code

File outputDir = context.getCacheDir(); // context should be the Activity pointer
File outputFile = File.createTempFile("prefix", "extension", outputDir);

Instead of String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/safco_private_pics");

For more details about cache dir read this: https://stackoverflow.com/a/6528104/7708305

But, If you want your file to be hidden and persisted on the permanent storage regardless of usage you can use following code.

File file = new File(root + "/.hidden_folder");

The dot(.) before the file/folder name makes it hidden.