Home > OS >  Asset folder contains PDF in Bengali Language and when uploading this PDF to Firebase it returns err
Asset folder contains PDF in Bengali Language and when uploading this PDF to Firebase it returns err

Time:07-28

I'm working on a project to upload PDF from asset to Firebase Storage, Some of PDFs are in Bengali Language when uploading these pdfs it returns error as "images".

Here is my code

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;
    }

This is error it returns.

D/ContentValues: Exception: images

CodePudding user response:

You need to provide path with the file name.
You can try this modified code snippet.

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;
    }
  • Related