如何解决Android 11 上的新 BlobStoreManager 读写
我以前使用外部存储来存储我想在我的应用程序之间共享的特定数据(没有任何内容提供者“主机”)
File folder = new File(Environment.getExternalStorageDirectory(),"FOLDER_NAME");
File file = new File(folder,"FILE_NAME.dat");
FileOutputStream outputStream = new FileOutputStream(file);
这就是我尝试使用 BlobStoreManager 的原因,正如 google 针对 30 (https://developer.android.com/training/data-storage/shared/datasets) 的建议中所建议的
读取和写入基于带有 4 个参数的 BlobHandle,一个是基于“内容”的 MessageDigest。 BlobHandle 必须使用相同的 4 个参数,否则读取将失败(SecurityException)。
我设法写入数据并读取它,但没有任何意义: 看来为了写,我需要用我想写的数据来生成BlobHandle。
然后,要读取,由于BlobHandle必须使用相同的4个参数,因此我也需要我写入的数据才能读取。 完全不合逻辑,因为我想读这些数据,我没有!
我一定错过了一些东西,或者只是不明白它是如何工作的。如果有人可以提供帮助:)
这是我的示例:
如果我设置如下:
- createBlobHandle: content = "mydata"
- 写入:data = "mydata"
- 然后写入会成功,读取也会成功。但是在正常用例中读取它之前我无法知道它的值:(
如果我设置以下内容(至少对我来说这是逻辑):
- createBlobHandle: content = "somekey"
- 写入:data = "mydata"
- 然后写入将失败:(
@RequiresApi(api = Build.VERSION_CODES.R)
private BlobHandle createBlobHandle() {
//Transfer object
String content = "SomeContentToWrite";
String label = "label123";
String tag = "test";
//Sha256 summary of the transmission object
try {
byte[] contentByte = content.getBytes("utf-8");
MessageDigest md = MessageDigest.getInstance("sha256");
byte[] contentHash = md.digest(contentByte);
return BlobHandle.createWithSha256(contentHash,label,tag);
} catch (NoSuchAlgorithmException e) {
e.printstacktrace();
} catch (UnsupportedEncodingException e) {
e.printstacktrace();
}
return null;
}
private void write() {
String data = "SomeContentToWrite";
@SuppressLint("WrongConstant") final BlobStoreManager blobStoreManager = ((BlobStoreManager) applicationContext.getSystemService(Context.BLOB_STORE_SERVICE));
//Generate the session of this operation
try {
BlobHandle blobHandle = createBlobHandle();
if (blobHandle == null)
return;
long sessionId = blobStoreManager.createSession(blobHandle);
try (BlobStoreManager.Session session = blobStoreManager.openSession(sessionId)) {
try (OutputStream pfd = new ParcelFileDescriptor.AutoCloSEOutputStream(session.openWrite(0,data.getBytes().length))) {
//The abstract of the written object must be consistent with the above,otherwise it will report SecurityException
Log.d(TAG,"writeFile: >>>>>>>>>>text = " + data);
pfd.write(data.getBytes());
pfd.flush();
//Allow public access
session.allowPublicAccess();
session.commit(applicationContext.getMainExecutor(),new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
//0 success 1 failure
Log.d(TAG,"accept: >>>>>>>>" + integer);
}
});
}
}
} catch (IOException e) {
e.printstacktrace();
}
}
private String read() {
String data = "";
@SuppressLint("WrongConstant") final BlobStoreManager blobStoreManager = ((BlobStoreManager) applicationContext.getSystemService(Context.BLOB_STORE_SERVICE));
BlobHandle blobHandle = createBlobHandle();
if (blobHandle != null) {
try (InputStream pfd = new ParcelFileDescriptor.AutoCloseInputStream(blobStoreManager.openBlob(createBlobHandle()))) {
//Read data
byte[] buffer = new byte[pfd.available()];
pfd.read(buffer);
String text = new String(buffer,Charset.forName("UTF-8"));
Log.d(TAG,"readFile: >>>>>>>>>>>>>>>>>>>>" + text);
} catch (IOException e) {
e.printstacktrace();
} catch (SecurityException e) {
e.printstacktrace();
}
}
return data;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。