我試圖將應用程序文件目錄中的圖像公開存儲在目錄中。通過公開,我的意思是我可以使用動作視圖意圖。android:公開存放目錄中的內部存儲文件
/** Saves the bitmap to app storage */
public int SaveBitmap(Bitmap bitmap, String filename, Boolean scan){
// Save the file
OutputStream os = null;
String path = context.getFilesDir() + "/MyAppName/";
File file = new File(path);
if(!file.isDirectory())file.mkdirs();
path += (filename + ".png");
try {
os = new BufferedOutputStream(new FileOutputStream(path,true));
//os = mContext.openFileOutput(, Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
bitmap.recycle();
} catch (IOException e) {
e.printStackTrace();
return 1; //Failure
}
File fileR = new File(path);
fileR.setReadable(true, false);
// Show in Gallery
if(scan) ShowInGallery(filename);
return 0; // SUCCESS
}
這段代碼不會做的工作,這些文件私下模式仍在產生。
使用:
context.openFileOutput(filename + ".png", Context.MODE_WORLD_READABLE);
我能夠公開打開它們,但我不能將文件保存在一個目錄。
請注意,我針對的用戶沒有SD卡,這就是爲什麼我沒有使用外部存儲。
我該怎麼辦?
如果重要的是圖像保持「私人」,除非採取行動,那麼您將需要實施一個「ContentProvider」來讓其他應用程序或意圖臨時訪問您的數據。如果所有圖像都可以訪問,您可以簡單地將它們存儲在外部,如下面解釋的答案。 – 323go
閱讀文檔'context.getFilesDir' – njzk2
@ njzk2 - 沒有幫助,那是內部存儲。 – katzenhut