2014-05-10 2 views
0

현재 앱을 만들고 있는데 "테스트"라는 폴더에서 내 앱에로드 할 데이터가 있습니다. 이 폴더는 이클립스에서 내 응용 프로그램의 디렉토리에 만들었으므로/res와/bin 다음에 그렇게 만들었다.Android 앱의 폴더

이제 내 앱에서이 폴더에 액세스하려고합니다. 그러나 나는 진전이 없다. 내가 지금까지 내 응용 프로그램의 경로를 얻고 있었다 했는가 :

testPath = getApplicationInfo().dataDir; 

그러나이 디렉토리에 "LIB"와 "캐시"라는 단 2 하위 폴더가 있습니다. 내 "테스트"폴더는 어디에서 찾을 수 있습니까?

+2

에 대한 자세한 내용은이 원시 내부 고해상도/자산이나 고해상도를/넣어 할 수 있습니다. AssetManager 및 Resources 클래스를 참조하십시오. –

+0

다른 방법이 있습니까? 내말은, 나는 단지 폴더를 읽고 싶다. AssetManager를 사용해야합니까? – PKlumpp

+1

Eclipse 프로젝트의 폴더가 디바이스의 폴더가되지 않습니다. 그것들은 apk 내부의 자원으로 J2SE 실행 가능한 jar 내부의 자원과 유사합니다. 왜 AssetManager 사용에 저항합니까? File과 마찬가지로 InputStream을 제공합니다. –

답변

1

당신은 다음과 같이 수행하십시오

public class ReadFileAssetsActivity extends Activity { 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TextView txtContent = (TextView) findViewById(R.id.txtContent); 
     TextView txtFileName = (TextView) findViewById(R.id.txtFileName); 
     ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets); 

     AssetManager assetManager = getAssets(); 

     // To get names of all files inside the "Files" folder 
     try { 
      String[] files = assetManager.list("Files"); 


      loop till files.length 

      { 
       txtFileName.append("\n File :"+i+" Name => "+files[i]); 
      } 


     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     // To load text file 
     InputStream input; 
     try { 
      input = assetManager.open("helloworld.txt"); 

      int size = input.available(); 
      byte[] buffer = new byte[size]; 
      input.read(buffer); 
      input.close(); 

      // byte buffer into a string 
      String text = new String(buffer); 

      txtContent.setText(text); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // To load image 
     try { 
      // get input stream 
      InputStream ims = assetManager.open("android_logo_small.jpg"); 

      // create drawable from stream 
      Drawable d = Drawable.createFromStream(ims, null); 

      // set the drawable to imageview 
      imgAssets.setImageDrawable(d); 
     } 
     catch(IOException ex) { 
      return; 
     } 
    } 
} 

을이

Read file from Assets

다른 튜토리얼 Open & Read File from Assets