[英]Android . Where to save downloaded images?
在我的應用程序中,可繪制文件夾中有幾張圖像。 這使apk大。 現在,我已將它們上傳到我的Google驅動器中,當用戶連接到互聯網時,它將從驅動器下載該圖像。 在哪里保存下載的圖像? 在外部存儲,數據庫還是其他地方? 我希望用戶無法刪除該圖像。
我希望用戶無法刪除該圖像
你真的不能那樣做。 除非用戶選擇了圖像,否則這些圖像不會出現在此處,那么為什么不希望用戶刪除它們? 在這種情況下,您將再次下載它或要求用戶選擇。 正常的“緩存”場景。 更不用說您實際上無法保護這些圖像,因為用戶始終可以清除應用程序數據(包括數據庫和內部存儲)。
您可以將它們存儲在內部電話存儲器中。
保存圖像
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("youDirName", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"img.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
訪問存儲的文件
private void loadImageFromStorage(String path)
{
try {
File f = new File(path, "img.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
// here is the retrieved image in b
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
編輯
您可以將文件直接保存在設備的內部存儲中。 默認情況下,保存到內部存儲器的文件是您的應用程序專用的,其他應用程序無法訪問它們(用戶也不能訪問它們)。 當用戶卸載您的應用程序時,這些文件將被刪除。
有關更多信息, 內部存儲
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.