1

您好我想使用简单的图片库,但现在是我在资产文件夹中的来源。如何将其更改为 SD 卡中的某个位置?

public class GalleryFileActivity extends Activity {

private GalleryViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String[] urls = null;
    List<String> items = new ArrayList<String>();
    try {
        urls = getAssets().list("");

        for (String filename : urls) 
        {
            if (filename.matches(".+\\.jpg")) 
            {
                String path = getFilesDir() + "/" + filename;
                copy(getAssets().open(filename), new File(path) );
                items.add(path);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {
            Toast.makeText(GalleryFileActivity.this, "Current item is " + currentPosition, Toast.LENGTH_SHORT).show();
        }
    });

    mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);
}
public void copy(InputStream in, File dst) throws IOException {

    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

}

我认为,问题出在这一行:“urls = getAssets().list("");" 谢谢你的帮助。

4

0 回答 0