android中把raw的文件拷贝到sd中

android中把raw的文件拷贝到sd中,代码如下:

InputStream istream = null;  
OutputStream ostream = null;
try {
                istream = context.getResources().openRawResource(Resource.getResourceId( assetsSrc,"raw" ) );
                ostream = new FileOutputStream(des);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = istream.read(buffer)) > 0) {
                    ostream.write(buffer, 0, length);
                }
                istream.close();
                ostream.close();
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    if (istream != null)
                        istream.close();
                    if (ostream != null)
                        ostream.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
注:Resource.getResourceId 是一个用文件名称反射获取 r.raw.xxx的工具类

代码如下:

//BasicApplication.getContext()上下文对象

class Resource{
public static int getResourceId(String name, String type) {
		if( name.equals( "" ) ) return 0 ;
		Resources themeResources = null;
		
		PackageManager pm = BasicApplication.getContext().getPackageManager();
		try {
			themeResources = pm.getResourcesForApplication(BasicApplication.getContext().getPackageName());
			return themeResources.getIdentifier(name, type, PackageUtil.getPackageName());
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}
}