如何解决下载文件而不减小android中的文件大小
在我的应用程序中,用户将加密的文件(.crypt
)上传到他们的Google驱动器,并提供可共享的链接。
我想做的就是下载文件并解密。但是问题是当我从一定数量下载文件减小文件大小(不知道;可能是下载时出现自动压缩或任何其他错误)。>
因为我无法使用密钥解密文件。
我尝试了以下方法来下载文件。
方法1
fun download(link: String,path: String) {
URL(link).openStream().use { input ->
FileOutputStream(File(path)).use { output ->
input.copyTo(output)
}
}
}
方法2
val downloadmanager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val uri: Uri = Uri.parse("https://drive.google.com/file/d/1pfaZSJDtex763-8vyTF-Z8uAl4dqAwMZ/view?usp=sharing")
val request = DownloadManager.Request(uri)
request.setTitle("new1")
request.setDescription("Downloading")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setVisibleInDownloadsUi(false)
request.setDestinationUri(Uri.parse("file://"+ Environment.getExternalStorageDirectory().toString()+"/new1.crypt/"))
downloadmanager.enqueue(request)
用于加密图像文件的方法
fun encryptFile(inputStream: InputStream,path: String): Boolean {
val fileOutputStream = FileOutputStream(
Environment.getExternalStorageDirectory().toString() + "/Eee.crypt/"
)
var key: ByteArray = ("123456789").toByteArray(charset("UTF-8"))
val messageDigest = MessageDigest.getInstance("SHA-1")
key = messageDigest.digest(key)
key = key.copyOf(16)
val secretKeySpec = SecretKeySpec(key,"AES")
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec)
val cipherOutputStream = CipherOutputStream(fileOutputStream,cipher)
var b: Int
val d = ByteArray(8)
while (inputStream.read(d).also { b = it } != -1) {
cipherOutputStream.write(d,b)
}
cipherOutputStream.flush()
cipherOutputStream.close()
inputStream.close()
val encryptedFile = File(
Environment.getExternalStorageDirectory().toString() + "/Eee.crypt/"
)
return encryptedFile.exists()
}
我要下载的示例加密文件在这里:https://drive.google.com/file/d/1pfaZSJDtex763-8vyTF-Z8uAl4dqAwMZ/view?usp=sharing
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。