android将assets下的文件写进sd卡

try
{
boolean sdCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED );
if ( !sdCardExist )
return;

File dirFile = new File( Environment.getExternalStorageDirectory() + "/misdktest" );
if(!dirFile.exists())
dirFile.mkdir();

File file = new File( Environment.getExternalStorageDirectory() + "/misdktest/testImg.png" );
if(file.exists())
return;

InputStream ins = context.getAssets().open( "testImg.png" );
FileOutputStream fos = new FileOutputStream( file );
byte[] buffer = new byte[1024];
int l = 0;
while( ( l = ins.read( buffer ) ) != -1 )
{
fos.write( buffer, 0, l );
}
fos.flush();
fos.close();
ins.close();
}
catch ( FileNotFoundException e )
{
e.printStackTrace();
}
catch ( IOException e )
{
e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/ahaitongxue/article/details/80407827
今日推荐