2013-08-30 108 views
1

你好我想使用简单的图片库,但现在是我的资源文件夹中的源。我该如何改变它,到SD卡中的某个地方?链接到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(); 
} 

}

我认为,问题是在这条线: “网址= getAssets()列表。(” “);”谢谢你的帮助。

+0

我没有在此计算机上的SDK。你可以发布一个Logcat,你有什么错误/问题?您是否拥有对您的权限的SD存储的读取和/或写入权限? RossC

+0

我有两个权限 我想从SD卡加载图像到这个画廊。 –

回答

0

您是否正在寻找资产目录中的复制内容到SD卡?

然后,请看一看这个链路Copying directories and files from res/raw folder to sd card - android

+0

我无法通过评论提问。因此,我张贴这么... – Yup

+0

不,我想从SD卡加载图像到这个画廊。 –

+0

'File dir = new File(“/ mnt/sdcard/images /”);''设置基址位置, '文件文件[] = dir.listFiles();' //循环 检查文件类型==图像并加载到galary – Yup