为啥图片没有上传到 Firebase-storage 并破解?

Posted

技术标签:

【中文标题】为啥图片没有上传到 Firebase-storage 并破解?【英文标题】:Why is the image not uploading to the Firebase-storage and craching?为什么图片没有上传到 Firebase-storage 并破解? 【发布时间】:2021-12-13 21:52:04 【问题描述】:

我制作了一个应用程序,它列出了包含(姓名、年龄、描述和照片)的人员列表,但问题是当我将数据记录在文件 (AddPerson) 中并保存时,应用程序崩溃了

这是崩溃日志:

E/StorageException: StorageException has occurred.
    User does not have permission to access this object.
     Code: -13021 HttpResult: 403
E/StorageException: The server has terminated the upload session
    java.io.IOException: The server has terminated the upload session
        at com.google.firebase.storage.UploadTask.serverStateValid(UploadTask.java:339)
        at com.google.firebase.storage.UploadTask.shouldContinue(UploadTask.java:308)
        at com.google.firebase.storage.UploadTask.run(UploadTask.java:229)
        at com.google.firebase.storage.StorageTask.lambda$getRunnable$7$com-google-firebase-storage-StorageTask(StorageTask.java:1072)
        at com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda3.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.io.IOException:   "error":     "code": 403,    "message": "Permission denied."  
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
        at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
        at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
        at com.google.firebase.storage.StorageTask.lambda$getRunnable$7$com-google-firebase-storage-StorageTask(StorageTask.java:1072) 
        at com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda3.run(Unknown Source:2) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 
    StorageException has occurred.
    User does not have permission to access this object.
     Code: -13021 HttpResult: 403
E/StorageException: The server has terminated the upload session
    java.io.IOException: The server has terminated the upload session
        at com.google.firebase.storage.UploadTask.serverStateValid(UploadTask.java:339)
        at com.google.firebase.storage.UploadTask.shouldContinue(UploadTask.java:308)
        at com.google.firebase.storage.UploadTask.run(UploadTask.java:229)
        at com.google.firebase.storage.StorageTask.lambda$getRunnable$7$com-google-firebase-storage-StorageTask(StorageTask.java:1072)
        at com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda3.run(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.io.IOException:   "error":     "code": 403,    "message": "Permission denied."  
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
        at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
        at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
        at com.google.firebase.storage.StorageTask.lambda$getRunnable$7$com-google-firebase-storage-StorageTask(StorageTask.java:1072) 
        at com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda3.run(Unknown Source:2) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 

这是AddPerson的代码:

public class AddPersonextends AppCompatActivity 

    private TextInputEditText mFirstName, mLastName, mAge, mDescription;

    private String firstNameString, lastNameString, ageString, descriptionString;
    private Uri imageUri = null;

    CardView mInsertImage;
    ImageView mImageView;

    private static final int IMAGE_PICK_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_person);

        mFirstName = findViewById(R.id.first_name_input);
        mLastName = findViewById(R.id.last_name_input);
        AboutMissingSpinner = findViewById(R.id.spinner_about_missing);
        mAge = findViewById(R.id.age_input);
        mDescription = findViewById(R.id.description_input);

        mInsertImage = findViewById(R.id.insert_image);
        mImageView = findViewById(R.id.imageView);


        mInsertImage.setOnClickListener(v -> pickImageFromGallery());
    

    private void pickImageFromGallery() 
        Intent i = new Intent();
        i.setType("image/*");
        i.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(i, IMAGE_PICK_CODE);
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 
        if (resultCode == RESULT_OK && requestCode == IMAGE_PICK_CODE) 
            assert data != null;
            imageUri = data.getData();
            mImageView.setImageURI(data.getData());
        
        super.onActivityResult(requestCode, resultCode, data);
    

    private void insertPerson() 
        firstNameString = Objects.requireNonNull(mFirstName.getText()).toString().trim();
        lastNameString = Objects.requireNonNull(mLastName.getText()).toString().trim();
        ageString = Objects.requireNonNull(mAge.getText()).toString().trim();
        descriptionString = Objects.requireNonNull(mDescription.getText()).toString().trim();

        if (TextUtils.isEmpty(lastNameString)) 
            lastNameString = "";
        

        if (TextUtils.isEmpty(firstNameString) && TextUtils.isEmpty(lastNameString)
                && TextUtils.isEmpty(ageString) && TextUtils.isEmpty(descriptionString)) 
            return;
        

        uploadImage();
    

    private void uploadImage() 
        long timestamp = System.currentTimeMillis();
        String filePathAndName = "images/" + timestamp;

        StorageReference storageReference = FirebaseStorage.getInstance().getReference(filePathAndName);
        storageReference.putFile(imageUri)
                .addOnSuccessListener(taskSnapshot -> 
                    Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                    while (!uriTask.isSuccessful());
                    String uploadedImageUri = "" + uriTask.getResult();
                    sendList(uploadedImageUri, timestamp);
                ).addOnFailureListener(e -> 

                );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_add_person, menu);
        return true;
    

    public boolean onOptionsItemSelected(MenuItem item) 
        if (item.getItemId() == R.id.action_save) 
            insertPerson();
            finish();
            return true;
         else if (item.getItemId() == android.R.id.home) 
            if (TextUtils.isEmpty(firstNameString) && TextUtils.isEmpty(lastNameString)
                    && TextUtils.isEmpty(ageString) && TextUtils.isEmpty(descriptionString)) 
            

            showUnsavedChangesDialog();
            return true;
        
        return super.onOptionsItemSelected(item);
    

    private void sendList(String uploadedImageUri, long timestamp) 
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("List");
        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("firstName", firstNameString);
        hashMap.put("lastName", lastNameString);
        hashMap.put("age", ageString);
        hashMap.put("description", descriptionString);
        hashMap.put("url", uploadedImageUri);
        hashMap.put("timestamp", timestamp);

        reference.push().setValue(hashMap);
    

【问题讨论】:

请同时添加崩溃日志 @SINDHYAPETER 我更新了帖子 我认为以正确的方式设置firebase权限是一个问题。请检查此链接是否有帮助:***.com/a/49991921/839042 @SINDHYAPETER 非常感谢,问题解决了 如果对您有帮助,请接受我的回答。 【参考方案1】:

这是因为 Firebase 存储设置了不正确的规则。您可以设置以下规则进行测试:

service firebase.storage 
  match /b/bucket/o 
    match /allPaths=** 
      allow read, write;
    
  

设置上述规则将在公开时开放。因此,您必须为生产设置适当的身份验证规则。

来自另一个答案https://***.com/a/49991921/839042的参考

【讨论】:

以上是关于为啥图片没有上传到 Firebase-storage 并破解?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Firebase 存储 URL 没有上传到 Google Cloud Firestore?

php上传文件到linux服务器,可以上传成功,但是图片本身为啥没有可执行权限呢,怎么设置啊

Word插入图片,为啥顺序是颠倒的?

为啥我不能从我自己的 iOS 应用程序将图片上传到我粘贴的事件?

求救!android部分手机上传照片到后台服务器为啥显示的图片是不全的,有缺失,缺失部分为黑色。

php为啥上传图片会失败