Thursday, July 23, 2015

getExternalFilesDirs() and getExternalCacheDirs()

Start from Android 4.4 (API Level 19), methods getExternalFilesDirs(String type) and getExternalCacheDirs() were introduced.

File[] getExternalFilesDirs (String type) -
Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

File[] getExternalCacheDirs () -
Returns absolute paths to application-specific directories on all external storage devices where the application can place cache files it owns. These files are internal to the application, and not typically visible to the user as media.

Example code run on RedMi 2 (Android 4.4.4), WITH external SD Card:
both /storage/emulated/0/...(internal memory) and /storage/sdcard1/...(SD Card) are listed.


Run on Nexus 7 (Android 5.1.1) WITHOUT external SD Card:


Example code:
com.example.androidexternalstorage.MainActivity.java
package com.example.androidexternalstorage;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends ActionBarActivity {

    TextView info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);

        File extFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        String strInfo =
            "Environment.getRootDirectory(): \n"
            + Environment.getRootDirectory() + "\n"
            + "Environment.getExternalStorageDirectory(): \n"
            + Environment.getExternalStorageDirectory() + "\n";

        strInfo +=
            "\ngetExternalFilesDir(null): \n"
            + getExternalFilesDir(null) + "\n"
            + "getExternalFilesDir(Environment.DIRECTORY_PICTURES): \n"
            + getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "\n"
            + "getExternalFilesDir(Environment.DIRECTORY_MUSIC): \n"
            + getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "\n";


        //API Level 19
        File[] externalFilesDirs = getExternalFilesDirs(null);
        strInfo += "\ngetExternalFilesDirs(null):\n";
        for(File f : externalFilesDirs){
            strInfo += f.getAbsolutePath() + "\n";
        }

        //API Level 19
        File[] externalCacheDirs = getExternalCacheDirs();
        strInfo += "\ngetExternalCacheDirs():\n";
        for(File f : externalCacheDirs){
            strInfo += f.getAbsolutePath() + "\n";
        }

        /*
        //API Level 21
        File[] externalMediaDirs = getExternalMediaDirs();
        strInfo += "\ngetExternalMediaDirs():\n";
        for(File f : externalMediaDirs){
            strInfo += f.getAbsolutePath() + "\n";
        }
        */

        info.setText(strInfo);
    }

}


layout/activity_main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>


</LinearLayout>


No comments: