资产文件夹包含孟加拉语的PDF,当将此PDF上传到Firebase时,会以图像形式返回错误



我正在做一个项目,将PDF从资产上传到Firebase Storage,有些PDF是孟加拉语的,上传这些PDF时会返回错误,错误为"图像";。

这是我的代码

private boolean listAssetFiles(String path) {
String [] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
else {
InputStream inputStream = getAssets().open(file);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[inputStream.available()];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
byte [] arr = buffer.toByteArray();
storageReference.child("PDFs").putBytes(arr).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.d(TAG, "Success = true: ");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Success = false ");
}
});
}
}
}
} catch (IOException e) {
Log.d(TAG, "Exception: "+e.getMessage());
return false;
}
return true;
}

这是它返回的错误。

D/ContentValues: Exception: images

您需要提供带有文件名的路径
您可以尝试此修改后的代码片段

private boolean listAssetFiles(String path) {
String[] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
else {
InputStream inputStream = getAssets().open(path+"/"+file);
int size = inputStream.available();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] arr = new byte[size];
while ((nRead = inputStream.read(arr, 0, arr.length)) != -1) {
buffer.write(arr, 0, nRead);
}
byte[] pdf = buffer.toByteArray();

storageReference.child("PDFs").child(file).putBytes(pdf).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "Uploaded: "+file, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Success = true: ");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Failed: "+file, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Success = false: ");
}
});
}
}
}
} catch (IOException e) {
Log.d(TAG, "listAssetFiles Failure: "+e.getMessage());
return false;
}
return true;
}

最新更新