0

Logcat shows error in these lines in memorymethod.java all errors related to getexternalpath() method which I given below in memorymothod.java code.

StatFs statFs2 = new StatFs(getExternalPath());
public static double getExternalUsedStorage()
{
    return Double.parseDouble(getExternalTotalMemory())-Double.parseDouble(getExternalFreeMemory());
}

Here is some code from memorymethod.java :

public static String getExternalPath()
{
     String sSDpath = null;
     File   fileCur = null;
     for( String sPathCur : Arrays.asList("MicroSD", "external_SD", "sdcard1", "ext_card", "external_sd", "ext_sd", "external", "extSdCard", "externalSdCard")) // external sdcard
     {
          fileCur = new File( "/mnt/", sPathCur);
          if( fileCur.isDirectory() && fileCur.canWrite())
          {
              sSDpath = fileCur.getAbsolutePath();
              break;
          }
          if( sSDpath == null)
          {
              fileCur = new File( "/storage/", sPathCur);
              if( fileCur.isDirectory() && fileCur.canWrite())
              {
                  sSDpath = fileCur.getAbsolutePath();
                  break;
              }
          }
          if( sSDpath == null)
          {
              fileCur = new File( "/storage/emulated", sPathCur);
              if( fileCur.isDirectory() && fileCur.canWrite())
              {
                  sSDpath = fileCur.getAbsolutePath();
                  //Log.e("path",sSDpath);
                  break;
              }               
        }
        fileCur = new File( "/storage/extSdCard");
        return fileCur.getAbsolutePath();
 }
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Shahroz javaid
  • 169
  • 1
  • 1
  • 12

4 Answers4

0

Check this code

String fileName="abc.txt";

File file = new File(fileName);

File dirAsFile = file.getParentFile();

use this code u will get path of ur file using external or internal storage path

Charuක
  • 12,953
  • 5
  • 50
  • 88
Rahul Karande
  • 259
  • 2
  • 9
  • Thanks rahul for your reply. I don't need to get file but i only want to show the external storage(used and total) in progress bar. i am developing booster app, – Shahroz javaid Dec 27 '16 at 12:33
  • public static long getExternalStorageAvailableSpace() { long availableSpace = -1L; try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory() .getPath()); stat.restat(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); } catch (Exception e) { e.printStackTrace(); } return availableSpace; } – Rahul Karande Dec 30 '16 at 07:04
0

Use this to get a particular file

File dir = new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/PersonData");
File file   =   new File(dir, "Data.pdf");

Here Data.pdf is the file name, inside my "PersonData" named folder in external storage

Ranjan
  • 1,326
  • 18
  • 38
0

I hope this will help you:

public static long getExternalStorageAvailableSpace() {
    long availableSpace = -1L;
    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()                .getPath());
        stat.restat(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Rahul Karande
  • 259
  • 2
  • 9
0

i thing you have not add this two permission in menifest

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

Rahul Karande
  • 259
  • 2
  • 9