Please Enable JavaScript!
Gon[ Enable JavaScript ]

반응형

안드로이드(android) 내장, 외장 메모리 사용가능여부와 크기 알아오기

 

개발환경 : window7 32bit, eclipse indigo, android API 1.7, platform 4.2

 

내장과 외장 메모리 정보를 알아오는 프로그램이다.

기기에는 테스트 해보지 않았지만 에뮬레이터에 설정해 놓은 값을

그대로 읽어 온다면 제대로 기능이 동작하는 소스가 될것이다.

아래 그림에서 보는것과 같이 Interal Storage : 200 MiB 이며

SD Card : 1024 MiB 로 설정이 되어있다.

 프로그램을 돌렸을때 나오는 값이다. 조금 차이는 있지만 거의 설정한 값과

동일함을 알수 있다. 화면에 나오는 값의 내용은 다음과 같다.

 

1. 전체 내장 메모리 사이즈

2. 사용가능한 내장 메모리 사이즈

3. 전체 외장 메모리(SD Card) 사이트

4. 사용가능한 외장 메모리(SD Card) 사이트

전체 소스는 다음과 같다. 주석이 달려 있으니 쉽게 알수 있으리라 생각이 된다.

layout xml 은 간단하기 때문에 소스를 보고 쉽게 작성할수 있으리라 생각되어

올리지 않았다.

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.os.StatFs;

import android.view.Menu;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

                    

       TextView txtInterTotal = (TextView) findViewById(R.id.txtInterTotal);

       TextView txtInterAvailTotal = (TextView) findViewById(R.id.txtInterAvailTotal);

       TextView txtExterTotal = (TextView) findViewById(R.id.txtExterTotal);

       TextView txtExterAvailTotal = (TextView) findViewById(R.id.txtExterAvailTotal);

                    

        txtInterTotal.setText("1.Total Interal Memory Size : " +

                            formatSize(getTotalInternalMemorySize()));

        txtInterAvailTotal.setText("2.Available Internal MemorySize : " +

                            formatSize(getInternalMemorySize()));

        txtExterTotal.setText("3.Total External MemorySize : " +

                            formatSize(getTotalExternalMemorySize()));

        txtExterAvailTotal.setText("4.Available External MemorySize : " +

                            formatSize(getExternalMemorySize()));

   }

          

   /** 전체 내장 메모리 크기를 가져온다 */

   private long getTotalInternalMemorySize() {

         File path = Environment.getDataDirectory();

         StatFs stat = new StatFs(path.getPath());

         long blockSize = stat.getBlockSize();

         long totalBlocks = stat.getBlockCount();

 

         return totalBlocks * blockSize;

    }

          

    /** 사용가능한 내장 메모리 크기를 가져온다 */

    private long getInternalMemorySize() {

          File path = Environment.getDataDirectory();

          StatFs stat = new StatFs(path.getPath());

          long blockSize = stat.getBlockSize();

          long availableBlocks = stat.getAvailableBlocks();

 

          return availableBlocks * blockSize;

    }

          

    /** 전체 외장 메모리 크기를 가져온다 */

    private long getTotalExternalMemorySize() {

          if (isStorage(true) == true) {

               File path = Environment.getExternalStorageDirectory();

               StatFs stat = new StatFs(path.getPath());

               long blockSize = stat.getBlockSize();

               long totalBlocks = stat.getBlockCount();

 

               return totalBlocks * blockSize;

          } else {

               return -1;

          }

     }

   

     /** 사용가능한 외장 메모리 크기를 가져온다 */

     private long getExternalMemorySize() {

          if (isStorage(true) == true) {

               File path = Environment.getExternalStorageDirectory();

               StatFs stat = new StatFs(path.getPath());

               long blockSize = stat.getBlockSize();

               long availableBlocks = stat.getAvailableBlocks();

               return availableBlocks * blockSize;

           } else {

               return -1;

           }

     }

          

    /** 보기 좋게 MB,KB 단위로 축소시킨다 */

    private String formatSize(long size) {

         String suffix = null;

 

         if (size >= 1024) {

             suffix = "KB";

             size /= 1024;

             if (size >= 1024) {

                  suffix = "MB";

                  size /= 1024;

             }

        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

 

        int commaOffset = resultBuffer.length() - 3;

        while (commaOffset > 0) {

             resultBuffer.insert(commaOffset, ',');

             commaOffset -= 3;

        }

 

         if (suffix != null) {

              resultBuffer.append(suffix);

         }

 

         return resultBuffer.toString();

    }

   

    /** 외장메모리 sdcard 사용가능한지에 대한 여부 판단 */

    private boolean isStorage(boolean requireWriteAccess) {

         String state = Environment.getExternalStorageState();

 

          if (Environment.MEDIA_MOUNTED.equals(state)) {

                return true;

          } else if (!requireWriteAccess &&

               Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

               return true;

          }

          return false;

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

          // Inflate the menu; this adds items to the action bar if it is present.

          getMenuInflater().inflate(R.menu.activity_main, menu);

          return true;

    }

}

반응형
Posted by 녹두장군1
,