Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ".nomedia" file and store images inside it Programmatically?

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;
}
like image 247
Rashid Kalhoro Avatar asked Dec 21 '18 07:12

Rashid Kalhoro


1 Answers

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();
}
like image 196
Amar Singh Avatar answered Sep 30 '22 14:09

Amar Singh