内部存储:
- 相关API:
- 读取文件:FileInputStream fis=openFileInput(“logo.png”)
- 保存文件:FileOutputStream fos=openFileOutput(“logo.png”,MODE_PRIVATE)
- 得到files文件对象:File fileDir=getFileDir()
- 操作assets下的文件:
- 得到AssetManager:context.getAssets()
- 读取文件:InputStream open(filename)
- 加载图片文件:Bitmap BitmapFactory.decodeFile(String pathName)
- 案例:保存与读取
- 保存步骤:
- 1,得到InputStream---->读取assets下的logo.png
- 2,/data/data/packageName/files/logo.png----->得到OutpStream
- 3,边读边写
- 5,提示
- 读取步骤:
- 1,得到图片路径
- 2,读取加载图片文件得到bitmap对象
- 3,将其设置到imagView中显示
注意:android studio的asserts是要自己手动创建的
在src目录下右键new–》folder—》Assets Folder
案例代码:
保持:
public void save(View view) throws IOException {
InputStream is= null;
FileOutputStream fos= null;
//得到AssertManager
AssetManager am=getAssets();
//读取文件
is = am.open("p1.jpg");
System.out.println(is+"---------------------------------");
//得到OutputStream
fos = openFileOutput("p1.jpg", Context.MODE_PRIVATE);
//边读边写
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
Toast.makeText(this,"保存",Toast.LENGTH_LONG).show();
}
读取:
public void read(View view){
String ap = getFilesDir().getAbsolutePath();
String imagePath=ap+"/p1.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);
}