Android 从文件夹中删除缓存的图像
我目前将从internet下载的图像保存到磁盘。我希望能够在用户关闭应用程序时删除图像。我希望所有的图像都保存在一个文件夹中,这样就可以很容易地删除它们。我做了Android 从文件夹中删除缓存的图像,android,android-activity,Android,Android Activity,我目前将从internet下载的图像保存到磁盘。我希望能够在用户关闭应用程序时删除图像。我希望所有的图像都保存在一个文件夹中,这样就可以很容易地删除它们。我做了getActivity().getCacheDir().getAbsolutePath()+File.separator+“newfoldername”来获取文件夹的路径。不确定如何将图像添加到文件夹中 public void saveImage(Context context, Bitmap b, String name_file, S
getActivity().getCacheDir().getAbsolutePath()+File.separator+“newfoldername”
来获取文件夹的路径。不确定如何将图像添加到文件夹中
public void saveImage(Context context, Bitmap b, String name_file, String path) {
FileOutputStream out;
try {
out = context.openFileOutput(name_file, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.JPEG,90, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Bitmap getImageBitmap(Context context, String name) {
try {
FileInputStream fis = context.openFileInput(name);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap b = BitmapFactory.decodeStream(fis,null, options);
b.getAllocationByteCount());
fis.close();
return b;
} catch (Exception e) {}
return null;
}
您不应该将图像保存在缓存文件夹中,因为根据文档,您不能依赖它。更好的方法是将它们存储在SD卡上。根据文件:
public abstract File getCacheDir()
返回文件系统上特定于应用程序的缓存目录的绝对路径。这些文件将是在设备存储空间不足时首先被删除的文件。无法保证这些文件将在何时被删除。注意:您不应依赖系统为您删除这些文件;对于缓存文件所占用的空间量,您应该始终有一个合理的最大值,例如1 MB,并在超过该空间时修剪这些文件。
用于保存
private String saveToInternalSorage(Bitmap bitmapImage){
File directory = getApplicationContext().getDir("MY_IMAGE_FOLDER"
,Context.MODE_PRIVATE);
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
private void loadImageFromStorage(String path)
{
try {
File file = new File(path, "Image.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
ImageView img = (ImageView)findViewById(R.id.my_imgView);
img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
用于阅读
private String saveToInternalSorage(Bitmap bitmapImage){
File directory = getApplicationContext().getDir("MY_IMAGE_FOLDER"
,Context.MODE_PRIVATE);
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
private void loadImageFromStorage(String path)
{
try {
File file = new File(path, "Image.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
ImageView img = (ImageView)findViewById(R.id.my_imgView);
img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
用于删除:
String dir = getApplicationContext().getDir("MY_IMAGE_FOLDER"
,Context.MODE_PRIVATE);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
String dir=getApplicationContext().getDir(“我的图像”文件夹)
,Context.MODE_PRIVATE);
if(dir.isDirectory()){
String[]children=dir.list();
for(int i=0;i
@AndroidWarrior-我如何阅读这些图像?当应用程序关闭时,我如何删除文件夹。@AndroidWarrior-就像从保存的图像中获取位图那样,我可以显示it@AndroidWarrior-我还想把照片保密。因此,无法使用文件找到它们manager@AndroidWarrior-非常感谢:)