0

我正在尝试使用从资产文件夹中获取的图像进行 Horizo​​ntalScrollview。我正在使用一个从外部存储中获取图像的示例,但我不知道我必须更改资产文件夹的路径。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myGallery = (LinearLayout)findViewById(R.id.mygallery);

    String ExternalStorageDirectoryPath = Environment
      .getExternalStorageDirectory()
      .getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath + "/test/";

    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();
    for (File file : files){
         myGallery.addView(insertPhoto(file.getAbsolutePath()));
    }    
}
4

1 回答 1

0

读取资产相当简单:

单张照片:

AssetManager assetManager = getResources().getAssets();
InputStream is = assetManager.open("photo.jpeg");

照片清单:

AssetManager  assetManager = getResources().getAssets();
String[] files =  assetManager.list("");
for(String f: files){
    File photo = new File(f); //or
    InputStream is = assetManager.open(f);
    //do something with the photo

}
于 2013-07-07T12:53:22.250 回答