2014-05-10 127 views
0

我目前正在开发一款应用程序,并且我有一些数据要从名为“tests”的文件夹加载到我的应用程序中。我在Eclipse中创建了这个文件夹,只是在我的应用程序的目录中,所以在/ res和/ bin等等旁边。Android应用程序中的文件夹

现在我想从我的应用程序访问此文件夹。但我没有任何进展。我做了什么,到目前为止是让我的应用程序的路径有:

testPath = getApplicationInfo().dataDir; 

但是在这个目录中,只有2子文件夹名为“LIB”和“缓存”。我在哪里可以找到我的“测试”文件夹?

+2

细节将其放在RES /资产或RES /生。请参阅AssetManager和资源类。 –

+0

有没有别的办法?我的意思是,我只想读一个文件夹。我真的必须使用AssetManager吗? – PKlumpp

+1

您的Eclipse项目中的文件夹不会成为设备上的文件夹。它们成为apk内的资源,类似于J2SE可执行jar内的资源。 为什么抵制使用AssetManager?它为您提供了一个InputStream,就像File一样。 –

回答

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

相关问题