Narendra Singh
Narendra Singh

Reputation: 4001

Removing app data from sd card on uninstallation of app

I am storing some of my app data like videos in external cache directory using the path

android.os.Environment.getExternalStorageDirectory(),"/Android/data/"+getPackag‌​eName()+"/cache/"

My expected result is removal of data from this specified path, when I uninstall my app. This is working well when I do it using emulator. The app data is removed from sdcard on uninstalling app. But, in the case, when I execute my code on real device, i.e. Samsung Galaxy S, the app data is not being removed on uninstall. Please, tell me it's possible reason, and how can I overcome this.

I have also tried to find the solution on duplicate threads, but that was of no use.

Upvotes: 0

Views: 3917

Answers (1)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data/<package_name>/files/

The <package_name> is your Java-style package name, such as "com.example.android.app". If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.

for more detail check this artical.

Edited:

check this i have just create one file in /Android/data/com.example.testdir/files/scanned.txt using below code:

package com.example.testdir;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void clickBtn(View v) {
        if (v.getId() == R.id.button1) {

            File sdcard = new File(Environment.getExternalStorageDirectory()
                    + "/Android/data/com.example.testdir/files/");

            if (!sdcard.exists())
                sdcard.mkdirs();

            File file = new File(sdcard, "scanned.txt");

            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
    }
}

and i have also add this permission

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

it remove folder name <package_name> in my case folder name is com.example.testdir from sdcard/android/data/com.example.testdir.

Upvotes: 4

Related Questions