Inputstream没有那个文件目录-Android
Inputstream no such file directory-Android
我从 Android 的内部存储中选择一个文本文件,例如 Sample.txt 使用 convertFileToByteArray( Uri documentUri)[= 将文件转换为字节数组14=]
Context applicationContext = MainActivity.getContextOfApplication();
private byte[] convertFileToByteArray(Context context, Uri documentUri) {
ByteArrayOutputStream buffer = null;
try {
InputStream inputStream = applicationContext.getContentResolver().openInputStream(documentUri);
int nRead;
byte[] data = new byte[16384];
buffer = new ByteArrayOutputStream();
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
} catch (Exception e) {
e.getLocalizedMessage();
}
return buffer.toByteArray();
}
在这一行
InputStream inputStream = applicationContext.getContentResolver().openInputStream(documentUri);
然而,没有捕捉到异常错误,当我选择一个无法识别其扩展名类型的文件时,它会捕捉到异常和 return "No such directory",例如 Sample.lev1,Sample.abc, Sample.rty
这个android应用程序是加密和解密的,所以我的加密文件扩展名很奇怪,比如.lev1, .abc, .rty。
加密很好,因为我选择了一个可读的文件(.txt、.pdf)但解密不行,因为当尝试将文件(.lev1、.abc、.rty)转换为字节数组时,它会抛出 "FileNotFoundException",
case R.id.btnGO://WILL CRYPT file
Uri doctUri = chosenFile;
keyType = MainActivity.keyType;
if (keyType == null || doctUri == null || radioGroup.getCheckedRadioButtonId() == -1) {
setMessage("Selection not Complete !");
} else {
mimeType = getMimeType(getContext(), doctUri);//check file whether it suitable or not.
// and to be used for save file
if (functionCode == ENCRYPT_FILE) {
try {
if (mimeType == null || isBCDMimeType(getFileName(doctUri))) {
if (isBCDMimeType(getFileName(doctUri))) {
//
} else {
}
} else {
byte[] b = convertFileToByteArray(doctUri);
launchRingDialogForEF(null, b, mimeType, keyType);
}
} catch (Exception e) {
e.getLocalizedMessage();
}
} else if (functionCode == DECRYPT_FILE) {
if (isVendorMimeType(getFileName(doctUri))) {
try {
byte[] b = convertFileToByteArray(doctUri);
size = b.length;
encryptDecryptHex ead = new encryptDecryptHex();
ead.decrypt(usbDeviceConnection, epOUT, epIN, b);
returnEData = ead.deData;
output = ead.decryptedData;
showMessageAfterTryToCrypt(returnEData, output, "Decrypt File", ead.m, ead.k);//m= mimeType k=keyType
goOk(usbDeviceConnection, epOUT, epIN);
} catch (Exception e) {
e.getLocalizedMessage();
}
} else {
}
}
}
如果 os 版本为 6.0 及更高版本,您在访问任何设备存储之前已在运行时获得权限。查看以下官方文档以获取更多信息
https://developer.android.com/training/permissions/requesting.html
Encrypted.ab(例如)的 uri 附加了 "%00"
content://com.android.externalstorage.documents/document/primary%3AEncrypted.a%00b%00%00%00%00%00
我从 Android 的内部存储中选择一个文本文件,例如 Sample.txt 使用 convertFileToByteArray( Uri documentUri)[= 将文件转换为字节数组14=]
Context applicationContext = MainActivity.getContextOfApplication();
private byte[] convertFileToByteArray(Context context, Uri documentUri) {
ByteArrayOutputStream buffer = null;
try {
InputStream inputStream = applicationContext.getContentResolver().openInputStream(documentUri);
int nRead;
byte[] data = new byte[16384];
buffer = new ByteArrayOutputStream();
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
} catch (Exception e) {
e.getLocalizedMessage();
}
return buffer.toByteArray();
}
在这一行
InputStream inputStream = applicationContext.getContentResolver().openInputStream(documentUri);
然而,没有捕捉到异常错误,当我选择一个无法识别其扩展名类型的文件时,它会捕捉到异常和 return "No such directory",例如 Sample.lev1,Sample.abc, Sample.rty
这个android应用程序是加密和解密的,所以我的加密文件扩展名很奇怪,比如.lev1, .abc, .rty。
加密很好,因为我选择了一个可读的文件(.txt、.pdf)但解密不行,因为当尝试将文件(.lev1、.abc、.rty)转换为字节数组时,它会抛出 "FileNotFoundException",
case R.id.btnGO://WILL CRYPT file
Uri doctUri = chosenFile;
keyType = MainActivity.keyType;
if (keyType == null || doctUri == null || radioGroup.getCheckedRadioButtonId() == -1) {
setMessage("Selection not Complete !");
} else {
mimeType = getMimeType(getContext(), doctUri);//check file whether it suitable or not.
// and to be used for save file
if (functionCode == ENCRYPT_FILE) {
try {
if (mimeType == null || isBCDMimeType(getFileName(doctUri))) {
if (isBCDMimeType(getFileName(doctUri))) {
//
} else {
}
} else {
byte[] b = convertFileToByteArray(doctUri);
launchRingDialogForEF(null, b, mimeType, keyType);
}
} catch (Exception e) {
e.getLocalizedMessage();
}
} else if (functionCode == DECRYPT_FILE) {
if (isVendorMimeType(getFileName(doctUri))) {
try {
byte[] b = convertFileToByteArray(doctUri);
size = b.length;
encryptDecryptHex ead = new encryptDecryptHex();
ead.decrypt(usbDeviceConnection, epOUT, epIN, b);
returnEData = ead.deData;
output = ead.decryptedData;
showMessageAfterTryToCrypt(returnEData, output, "Decrypt File", ead.m, ead.k);//m= mimeType k=keyType
goOk(usbDeviceConnection, epOUT, epIN);
} catch (Exception e) {
e.getLocalizedMessage();
}
} else {
}
}
}
如果 os 版本为 6.0 及更高版本,您在访问任何设备存储之前已在运行时获得权限。查看以下官方文档以获取更多信息 https://developer.android.com/training/permissions/requesting.html
Encrypted.ab(例如)的 uri 附加了 "%00"
content://com.android.externalstorage.documents/document/primary%3AEncrypted.a%00b%00%00%00%00%00