繁体   English   中英

Firebase 在 Android 中存储没有用于请求的身份验证令牌

[英]Firebase Storage no auth token for request in Android

我不知道如何描述这个问题,所以我将列出我需要为 app 执行的任务:

  1. 从 Firebase 云存储下载 a.txt 文件
  2. 将其存储在我的应用程序特定存储目录中
  3. 从特定于应用程序的存储中获取文件并逐行读取。

日志猫:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/2328/ORIGINAL/NONE/image/png/1631689393 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F2328/ORIGINAL/NONE/image%2Fpng/1631689393} }} to activity {com.example.android.getroasted/com.example.android.getroasted.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.observe(androidx.lifecycle.LifecycleOwner, androidx.lifecycle.Observer)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.observe(androidx.lifecycle.LifecycleOwner, androidx.lifecycle.Observer)' on a null object reference

分段:

private void setRoast() throws FileNotFoundException {
        homeViewModel.getRandomRoast().observe(getViewLifecycleOwner(), new Observer<byte[]>() {
            @RequiresApi(api = Build.VERSION_CODES.Q)
            @Override
            public void onChanged(byte[] bytes) {
                try (FileOutputStream fos = requireContext().openFileOutput("roasts", Context.MODE_PRIVATE)) {
                    fos.write(bytes);
                } catch (IOException e) {
                    Log.d(TAG, "IOException: " + e);
                }
            }
        });

        roastContainer.setVisibility(View.VISIBLE);
        FileInputStream fis = getContext().openFileInput("roasts");
        InputStreamReader inputStreamReader =
                new InputStreamReader(fis, StandardCharsets.UTF_8);
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
            String line = reader.readLine();
            while (line != null) {
                stringBuilder.append(line).append('\n');
                line = reader.readLine();
                Log.d(TAG, "roast: " + line);
            }
        } catch (IOException e) {
            // Error occurred when opening raw file for reading.
        } finally {
            String contents = stringBuilder.toString();
        }
    }

视图模型:

public class HomeViewModel extends ViewModel {

    private static final String TAG = "HomeViewModel";
    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
    StorageReference storageReference = firebaseStorage.getReference();
    private StorageReference roastDocRef = storageReference.child("roasts.txt");
    private MutableLiveData<byte[]> roastLiveData;

    public MutableLiveData<byte[]> getRandomRoast() {
        roastDocRef.getBytes(1024*1024)
                .addOnSuccessListener(new OnSuccessListener<byte[]>() {
                    @Override
                    public void onSuccess(byte[] bytes) {
                        Log.d(TAG, "bytes: " + bytes);
                        roastLiveData.postValue(bytes);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "could not download the roast doc " + e);
            }
        });
        return roastLiveData;
    }
}

我不知道问题发生的原因或位置,但我已经尝试过这些事情:

  • 检查了我的安全规则,它们允许读、写
  • 将 viewmodel.setValue() 更新为viewmodel.postValue ()
while (line.hasNext) {
               stringBuilder.append(line).append('\n');
               line = reader.readLine();
               Log.d(TAG, "roast: " + line);
           }

试试这个代码

其次,试试我的代码

File textfile = new File(address of file);
if (textFile.exists()) {
            // if file already exists
            // Reading data in text file 

            StringBuilder textdata = new StringBuilder("");
            try {
                Scanner myReader = new Scanner(textFile);
                while (myReader.hasNextLine()) {
                    String data = myReader.nextLine();
                    textdata.append(data);

                }
                myReader.close();
            } catch (FileNotFoundException e) {
                Log.d("vijaySee", "textWork: error in reading FIle");
                e.printStackTrace();
            }

//now textdata String has all the data for the user.

确保首先下载文件,然后读取 firebase 存储参考的 onsuccessListener 中的数据。 在下载文本文件之前读取可能会导致 null 指针异常。

首先,只需下载文件,然后通过将实际地址放在括号中来使用我的。 然后 textData 变量将包含您需要的字符串中的所有数据。

这与身份验证令牌或安全规则无关。

错误消息表明getRandomRoast()返回 null。 事实上,它确实这样做了,因为您从未为roastLiveData分配一个值。 它将始终是 null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM