6

For my Application images are stored in my phone's internal storage and images are visible in the gallery, but my client wants images will not be visible inside the gallery.

I added .nomedia file manually inside the folder where images are stored and it disappears but I took new image again it is visible in the gallery.

So how can I do it programmatically so that images will not appear in my gallery?

Here is my code.

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 );
            imageCustomer.setImageBitmap(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);
            Log.e(TAG, "image: "+ imageTemplateStr );

            imageCustomer.setImageBitmap(bmp);
        }

}

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;
}

compressImage() method .

public static Bitmap compressImage(File imgFile, Context context) {
    Bitmap compressedImgBitmap = new Compressor.Builder(context)
            .setMaxWidth(640)
            .setMaxHeight(480)
            .setCompressFormat(Bitmap.CompressFormat.PNG)
            .build()
            .compressToBitmap(imgFile);


    return compressedImgBitmap;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rashid Kalhoro
  • 340
  • 4
  • 14
  • 1
    create folder programmatically before set dot(.) – Pratik18 Dec 21 '18 at 07:38
  • @Pratik18 thanks but if you share code for creating .nomedia file or how can i Create a folder in 'Android' folder(where data & obb folders are present) then it will be ignored by gallery – Rashid Kalhoro Dec 21 '18 at 07:47

2 Answers2

8

Create .nomedia file in Storage for hide audio from media player:

String filepath = Environment.getExternalStorageDirectory().getPath()+"/";

File file = new File(filepath+".nomedia");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Amar Singh
  • 435
  • 5
  • 9
0
File files = getExternalFilesDir("");
File root = new File(files.getAbsolutePath()+"/Saveimags");
  if (!root.exists()) {
     root.mkdirs();
  }
File gpxfile = new File(root, ".nomedia");
FileWriter writer = new FileWriter(gpxfile);
writer.flush();
writer.close();


       File path = new File(root, "dump.png");
            try {
                FileOutputStream out = new FileOutputStream(path);
                Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
                myBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

Android/data/your package name/files/ this is your Android data folder path after you can save image in this folder.

Pratik18
  • 365
  • 1
  • 7