我自己的一种做法,结合了wide_n以及网上一些其他人的提供的资料:
InputStream is;
tmpFile = new File(getExternalFilesDir(null), "100ss_l.png"); //该路径指向SD卡目录下一个特定文件夹,需要在uses-permission设置WRITE-EXTERNAL-STORAGE权限
Log.w("File", "File path is: " + path);
try {
is = getClass().getResourceAsStream("/assets/www/images/100ss_l.png");
if(!tmpFile.exists()){
FileOutputStream fos = new FileOutputStream(tmpFile);
byte[] buffer = new byte[400000];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
}
is.close();
} catch (IOException e) {
Log.w("ExternalStorage", "Error writing " + tmpFile, e);
e.printStackTrace();
}
Uri uri = Uri.fromFile(tmpFile);
Log.w("Uri", "Uri path is: " + uri);
uris.add(uri);
图片被输出到一个临时文件中,在程序结束时可以删除文件:
@Override
protected void onDestroy(){
super.onDestroy();
//delete temp file
if(tmpFile.exists())
tmpFile.delete();
}
顺便请教一下各位高手:
我见过一些程序写法如下:
getClass().getClassLoader().getResourceAsStream("/assets/www/images/100ss_l.png");
这里的getClassLoader()方法有什么作用呢?包括API DOC里也是推荐要加。但我加上这个反而取不到文件了。哪位了解的请指点一下。