当前位置: 首页 > 知识库问答 >
问题:

StorageException已发生。对象的位置不存在。代码:-13010 httpresult:404

司宏伯
2023-03-14

StorageException已发生。该对象不存在于该位置。代码:-13010 httpresult:404

我找到了以前的大部分问题和答案,但它们对我不起作用。提前谢了。

package com.pk.hotandcool.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fxn.pix.Options;
import com.fxn.pix.Pix;
import com.fxn.utility.ImageQuality;
import com.fxn.utility.PermUtil;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageMetadata;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.pk.hotandcool.R;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;

public class Test extends AppCompatActivity {

    private static final String TAG = "Test";
    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    private Options options;
    private ArrayList<String> returnValue = new ArrayList<>();
    private GridLayoutManager gridLayoutManager;
    int imageCount = 0;
    private FirebaseAuth mAuth;
    private String currentUid;
    private DatabaseReference userMediaDb;
    private StorageReference filePath;
    private StorageMetadata metadata;

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

        initViews();
        initFirebase();

        recyclerView.setLayoutManager(gridLayoutManager);
        myAdapter = new MyAdapter(this);

        options = Options.init()
                .setRequestCode(100)
                .setCount(9)
                .setFrontfacing(false)
                .setImageQuality(ImageQuality.LOW)
                .setPreSelectedUrls(returnValue)
                .setScreenOrientation(Options.SCREEN_ORIENTATION_PORTRAIT)
                .setPath("DCIM/pk");

        recyclerView.setAdapter(myAdapter);

        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                options.setPreSelectedUrls(returnValue);
                Pix.start(Test.this, options);
            }
        });
    }

    private void initFirebase() {
        currentUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        userMediaDb = FirebaseDatabase.getInstance().getReference().child("Users")
                .child(currentUid)
                .child("media");
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);

        gridLayoutManager = new GridLayoutManager(this, 3);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == Activity.RESULT_OK) {
                returnValue = data.getStringArrayListExtra(Pix.IMAGE_RESULTS);
                if (returnValue != null) {
                    myAdapter.addImages(returnValue);
                    if (returnValue.size() > 0) {
                        uploadMediaToFirebase(returnValue);
                    }
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NotNull String[] permissions, @NotNull int[] grantResults) {
        if (requestCode == PermUtil.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {// If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Pix.start(Test.this, options);
            } else {
                Toast.makeText(Test.this, "Approve permissions to open Pix ImagePicker", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }


    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private ArrayList<String> list = new ArrayList<>();
        private Context context;

        MyAdapter(Context context) {
            this.context = context;
        }

        void addImages(ArrayList<String> list) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }

        @NotNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.pix_image_layout, parent, false);
            return new Holder(v);
        }

        @Override
        public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
            File f = new File(list.get(position));
            Bitmap d = new BitmapDrawable(context.getResources(), f.getAbsolutePath()).getBitmap();
            ((Holder) holder).iv.setImageBitmap(d);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            ImageView iv;

            Holder(View itemView) {
                super(itemView);
                iv = itemView.findViewById(R.id.iv);
            }
        }
    }

    private void uploadMediaToFirebase(ArrayList<String> images) {

        filePath = FirebaseStorage.getInstance().getReference()
                .child("users")
                .child(currentUid)
                .child("media");

        metadata = new StorageMetadata.Builder()
                .setContentType("image/jpg")
                .build();

        for (int i = 0; i < images.size(); i++) {
            String image = images.get(i);
            final File file = new File(image);
            uploadImages(filePath, metadata, file);
        }
    }

    private void uploadImages(final StorageReference filePath, StorageMetadata metadata, File file) {
        UploadTask uploadTask = filePath.child(file.getName()).putFile(Uri.fromFile(file), metadata);

        uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "onComplete: uploaded successfully" + task.getResult().toString());
                } else {
                    Log.d(TAG, "onComplete: upload failed" + task.getResult().toString());
                }
            }
        });

        Task<Uri> task = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    Log.d(TAG, "then: " + task.getResult());
                    if (task.getException() != null)
                        throw task.getException();
                }
                Log.d(TAG, "then: " + filePath.getDownloadUrl().toString());
                return filePath.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    Log.d(TAG, "onComplete: download uri" + downloadUri.toString());
                } else {
                    if (task.getException() != null)
                        Log.e(TAG, "onComplete: getting url failed");
                    Log.d(TAG, "onComplete: download url : " + task.getResult());
                    task.getException().printStackTrace();
                }
            }
        });
    }
}

共有1个答案

楚方伟
2023-03-14

我犯了一个错误,我返回了task.getdownloadurl()而不是reference.getdownloadurl,所以我更改了行

UploadTask uploadTask = filePath.child(file.getName()).putFile(Uri.fromFile(file), metadata);

进入

StorageReference reference = filePath.child(file.getName());
UploadTask uploadTask = reference.putFile(Uri.fromFile(file), metadata);

返回

return filePath.getDownloadUrl();
 类似资料:
  • 这是我的logcat: 2018-11-04 08:54:03.257 205 12-20582/com.example.nabil.loginDemo E/StorageException:StorageException已发生。对象的位置不存在。代码:-13010 httpresult:404 201 8-11-04 08:54:03.268 205 12-20582/com.example.

  • 我已经尝试了StackOverflow上发布的所有解决方案,但我的问题没有在那里讨论,所以我提出了这个问题。在我的例子中,图像被上传到firebase存储桶中,但没有在firestore中插入新记录,因此发生了此错误。我的代码:- 存储数据函数如下所示:-

  • 在创建用户配置文件的时候,我试图为我的用户使用SetPhotori,我使用两个seperate方法首先上传,然后获取url,这两个方法都是从“CreateUserWithEmailandPassword”方法中调用的,但我得到了这个错误消息:com.google.firebase.storage.StorageException:Object不存在。 //如果我使用ref.toString(),结

  • 我有两个程序。第一个分配共享内存文件,第二个从中读取。。我使用placement new将对象放置到此内存中,以确保对象不会使用新的或分配共享内存文件之外的任何内存。 我的阵列结构: 方案1: 方案2: > 程序一将SHMArray放置在内存中,位置为new。程序二在程序一已经放置的对象上做同样的事情(覆盖它)。这是未定义的行为吗?我认为不是,但我想确认一下。 两个程序都不调用析构函数数组- 我基

  • 我有一个与我的CSS有关的div位置的问题。 我的父div设置为,子div设置为 但由于某些原因,子div显示在父div的边界下方和外部。。。 这是我的CSS: 超文本标记语言: 我提供了一个JSFiddle来向您展示它的实际操作: http://jsfiddle.net/j6VLc/1/ 在使用时,如何修复它以使子div位于父div内?