如何将相机图像(以及在电话库中选择的图像)保存到Firebase

问题描述 投票:1回答:1

所以我实际上是这两种方法来保存firebase上的图像,这是使用相机图像和从图库中选择图像。

app ui

当我从图库中选择图像时,单击按钮保存我可以立即保存。但是当我单击相机按钮然后单击按钮保存时,该按钮不起作用。这意味着我没有在其中添加任何代码。

这是上传图片的方法:

    private void uploadImage () {

    if (filePath != null) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading..");
        progressDialog.show();

        StorageReference ref = storageReference.child("winepic/" + UUID.randomUUID().toString());
        ref.putFile(filePath)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        String name = inputName.getText().toString().trim();
                        String type = inputType.getText().toString().trim();
                        String price = inputPrice.getText().toString().trim();
                        String description = inputDescription.getText().toString().trim();

                        if (!TextUtils.isEmpty(name)) {

                            String id = databaseWine.push().getKey();

                            Wine wine = new Wine(id, name, type, price, description, taskSnapshot.getDownloadUrl().toString());

                            databaseWine.child(id).setValue(wine);

                            //Toast.makeText(AddWine.this, "Added", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(AddWine.this, "You should enter name", Toast.LENGTH_SHORT).show();
                        }

                        progressDialog.dismiss();
                        Toast.makeText(AddWine.this, "Uploaded", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(AddWine.this, ListWine.class);
                        startActivity(i);

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                        progressDialog.dismiss();
                        Toast.makeText(AddWine.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());
                        progressDialog.setMessage("Uploaded "+(int)progress+"%");

                    }
                });

    }
}

这是OnActivityResult的代码:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK){

        if(requestCode == CAPTURE_IMAGE_REQUEST) {

                Bundle bundle = data.getExtras();
                final Bitmap bmp = (Bitmap) bundle.get("data");
                imageView.setImageBitmap(bmp);

        }else if(requestCode == PICK_IMAGE_REQUEST){

            filePath = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

我希望你们能帮我解决这个问题。在先进的感谢,祝你有个美好的一天!

javascript android firebase firebase-realtime-database
1个回答
0
投票

问题:从相机捕获图像时,您没有保存filepath

解决方案:将代码更改为:

  if(requestCode == CAPTURE_IMAGE_REQUEST) {

            Bundle bundle = data.getExtras();
            final Bitmap bmp = (Bitmap) bundle.get("data");
            imageView.setImageBitmap(bmp);
            filePath = getRealPathFromURI(getContext(),bmp);
    }

将此方法添加到您的班级

 public Uri getRealPathFromURI(Context context,Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage,
                "Title", null);
        Cursor cursor = context.getContentResolver().query(Uri.parse(path), null, null, 
                null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return  Uri.fromFile(new File(cursor.getString(idx)));
    }

© www.soinside.com 2019 - 2024. All rights reserved.