下面的代码暂时存储文件(我想在缓存中)但是我需要永久存储文件,我该怎么办?
public void downloadFile(){
StorageReference islandRef = storageRef.child("cliff.jpg");
File localFile = null;
try {
File cacheDir = getApplicationContext().getCacheDir();
localFile = File.createTempFile("images", ".jpg", cacheDir);
} catch (IOException e) {
e.printStackTrace();
}
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.i("msg", exception.toString());
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:0)
我们说话有多久?如果应用程序已卸载,数据是否仍然存在?一种解决方案是将数据存储在内部存储中(只有应用程序可访问,并在卸载应用程序时删除),方法是更改:
func obtainKeyData(tag: String) -> NSData? {
var keyRef: AnyObject?
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecReturnData): kCFBooleanTrue as CFBoolean,
String(kSecClass): kSecClassKey as CFStringRef,
String(kSecAttrApplicationTag): tag as CFStringRef,
]
let result: NSData?
switch SecItemCopyMatching(query, &keyRef) {
case noErr:
result = keyRef as? NSData
default:
result = nil
}
return result
}
func insertPublicKey(publicTag: String, data: NSData) -> SecKeyRef? {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecClass): kSecClassKey as CFStringRef,
String(kSecAttrApplicationTag): publicTag as CFStringRef,
String(kSecValueData): data as CFDataRef,
String(kSecReturnPersistentRef): true as CFBooleanRef]
var persistentRef: AnyObject?
let status = SecItemAdd(query, &persistentRef)
if status != noErr && status != errSecDuplicateItem {
return nil
}
return obtainKey(publicTag)
}
func obtainKey(tag: String) -> SecKey? {
var keyRef: AnyObject?
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecReturnRef): kCFBooleanTrue as CFBoolean,
String(kSecClass): kSecClassKey as CFStringRef,
String(kSecAttrApplicationTag): tag as CFStringRef,
]
let status = SecItemCopyMatching(query, &keyRef)
switch status {
case noErr:
if let ref = keyRef {
return (ref as! SecKeyRef)
}
default:
break
}
return nil
}
到
File cacheDir = getApplicationContext().getCacheDir();
答案 1 :(得分:0)
如果你想要一个你自己和其他应用程序可以轻松访问的文件,你应该获得外部存储的根目录并在那里创建文件夹......就像这样......
File externalRoot = Environment.getExternalStorageDirectory();
File dir = new File (externalRoot .getAbsolutePath() + "/parentFolder/childFolder");
dir.mkdirs();
File file = new File(dir, "filename");
...
FileOutputStream f = new FileOutputStream(file);
...
请记住,您应该检查存储是否可写。这将是其他应用程序可见的,如果您不想要这个
使用getApplicationContext().getFilesDir();
来提供/data/data/com.package/files
答案 2 :(得分:0)
为了保存图像,您必须通过这样做将其写入内部或外部存储:
而不是:
File cacheDir = getApplicationContext().getCacheDir();
localFile = File.createTempFile("images", ".jpg", cacheDir);
使用:
File cacheDir = getApplicationContext().getFilesDir();
localFile = new File("images.jpg", cacheDir);
我不知道firebase的StorageReference
如何工作,但我推断.getFile
将内容流式传输到文件中。在这种情况下,此代码应该起作用