从服务器下载文件的字节后。我努力将这些字节转换为文件并使用该文件。我似乎无法得到我做错的事。
我如何将字节转换为文件。
public File putFile(byte[] b) throws IOException {
final File f = new File(A.getDir("my_custom_file", Context.MODE_PRIVATE),
"downloads");
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.flush();
out.close();
return f;
}
打开文件的方法。
private void openFile(Uri uri, String ext) {
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(ext);
newIntent.setDataAndType(uri, mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
A.startActivity(newIntent);
}
打开文件:
Uri uri = Uri.fromFile(putFile(bytes));
openFile(uri, "extension");
我做错了什么?
编辑:无论如何,我使用firebase下载文件。
FirebaseStorage S = FirebaseStorage.getInstance();
StorageReference storageRef = S.getReferenceFromUrl("gs://...");
storageRef = storageRef.child(ChildRef);
final long ONE_MEGABYTE = 1024 * 1024;
storageRef.getBytes(ONE_MEGABYTE * 7).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
//...
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//..
}
});