【问题标题】:How to store image files into FirebaseStorage如何将图像文件存储到 FirebaseStorage
【发布时间】:2019-05-26 04:58:10
【问题描述】:

当我初始化 mStorageRef = FirebaseStorage.getInstance().getReference("uploads"); mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");,我的应用程序已停止 我的安卓 sdk 版本 28; buildToolsVersion '28.0.3'

致命例外: 解析失败:Lcom/google/android/gms/common/internal/zzbq; 在 com.google.firebase.storage.FirebaseStorage.getInstance(未知来源:11)

【问题讨论】:

    标签: android


    【解决方案1】:

    我不明白你在这里做错了什么,让我给你写完整的代码来解决你的问题。

    FirebaseStorage firebaseStorage;
    StorageReference storageReference;
    
    private Button btnChoose, btnUpload;
    private ImageView imageView;
    
    private Uri filePath;
    
    private final int IMAGE_REQUEST = 71;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();
        btnChoose = (Button) findViewById(R.id.btnChoose);
        btnUpload = (Button) findViewById(R.id.btnUpload);
        imageView = (ImageView) findViewById(R.id.imgView);
    
        btnChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });
    
        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });
    
    }
    
    private void chooseImage() {
        //an intent to choose your required image.
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select your photo."), IMAGE_REQUEST);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null) {
            filePath = data.getData();
            //this will update your imageView
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageView.setImageBitmap(bitmap);
        }
    }
    
    private void uploadImage() {
    
        if (filePath != null) {
            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("Uploading your image");
            progressDialog.show();
    
    
            //Put uid of the user to make it easy to get your image back when needed. I would suggest you to use an external
            //library to compress the image for best performance.
            StorageReference ref = storageReference.child("uploads/" + "PUT_UID_OF_USER");
    
            //method to upload a file.
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            //if the image is successfully uploaded
                            progressDialog.dismiss();
                            Toast.makeText(Activity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            //if image upload was failed due to some reason.
                            progressDialog.dismiss();
                            Toast.makeText(Activity.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
                                    .getTotalByteCount());
    
                            //this will actually show the progress in the progress dialog.
                            progressDialog.setMessage("Uploaded " + (int) progress + "%");
                        }
                    });
        }
    }
    
    //use picasso library for this for the best performance/result.
    private void getImageBack(String uid_of_user){
    
        StorageReference ref = storageReference.child("uploads/" + "PUT_UID_OF_USER").getImage("PUT_NAME_OF_IMAGE");
    
        load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                // Got the download URL for 'users/me/profile.png'
                // Pass it to Picasso to download, show in ImageView and caching
                Picasso.with(Test.this).load(uri.toString()).into(imageView);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle any errors
                Toast.makeText(Test.this, "failed", Toast.LENGTH_SHORT).show();
            }
        });
    
    
    }
    

    代码未经测试,可能出现错误,您可以在任何需要时使用我的帮助:)

    谷歌搜索得到毕加索图书馆,这是一个非常有名的图书馆。

    【讨论】:

    • 我的意思是,我将 API 级别从 API 27:Android 8.1 (Oreo) 更改为 API 28,只要我初始化 StorageReference ref = storageReference.child("uploads/" + "PUT_UID_OF_USER")。 getImage("PUT_NAME_OF_IMAGE");我的应用突然停止并崩溃了。
    • 考虑发布您的日志数据。
    猜你喜欢
    • 2017-06-19
    • 2021-12-23
    • 2021-03-19
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多