简体   繁体   中英

Accessing internal/external memory in android

I am pretty new to android programming and I need your help in proceeding further in my application. I wanted to access the internal or external memory of android phones through my application with both write and read permissions. I wanted to give users a choice as to which memory to be used in the application.

I'll be thankful to anyone who can help me.

To check the sdCard is present in android device:

android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

Get the sd card's root directory:

Environment.getExternalStorageDirectory();

to store in to your phone memory into your package data folder:

String pathOfRoot = "/data/data/" + getPackageName();

Hope it will help you.

For External storage Use this:

File folder = new File(Environment.getExternalStorageDirectory() + "/dirName" );
        if(!folder.exists())
        {
            folder.mkdir();
            Log.i("Log", "folder created");
        }

This will create a folder with the name dirName into your sdcard . This means you get the access of the external storage by this Environment.getExternalStorageDirectory() method.

But for this you have to add the permission into your menifest file like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM