Search code examples
androidandroid-sdcard

How to delete SDcard file in Android


I tried to delete the image file from the gallery, but it won't. Image files are output normally, and sharing functions are done. It can't write and delete in my App. Files are deleted from the default app. I tried to delete it use File class and ContentResolver. but file has not been deleted. Android targetSdkVersion is 26 and compileSdkVersion is 28.

Manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I get SDcard path from getSDcardPath()

public String getSDcardPath(Context context) {
   File[] storage = ContextCompat.getExternalFileDirs(context, null);

   if(storage.length > 1 && storage[0] != null && storage[1] != null)
     return storage[1].toString();
   else
     return "";
}

File Class code Used

public void useFileClass() {
   File mFile = new File("file Parent + file NAME");

   if (mFile.exists()) {
   mFile.delete();
   }
}

ContentResolver code Used

public void useContentResolver(Context context, File mFile) {

   ContentResolver contentResolver = context.getContentResolver();
   Uri mUri = getUri(context, mFile);

   contentResolver.delete(mUri, null, null);
}

public Uri getUri(Context context, File mFile) {
   Uri mUri;

   mUri = FileProvider.getUriForFile(context, "MyApplication", mFile);
   return mUri;
}

share code

public void shareImage() {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, getUri(this, mFile));

        startActivity(Intent.createChooser(shareIntent, "Share image too..."));
    }

Solution

  • You need request Permission before access into storage. Try this:

    private static final int MY_WRITE_STORAGE_PERMISSION_CODE = 200;    
    
    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_WRITE_STORAGE_PERMISSION_CODE);
            } else {
                // Todo (Add, Delete, Edit, ...)
            }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
                @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
    
        if(requestCode == MY_WRITE_STORAGE_PERMISSION_CODE)
        {
            if (ContextCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                // Todo (Add, Delete, Edit, ...)
            } else {
                // Permission Deny
            }
        }
    }
    

    Hope this help you.