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

时间:2018-03-29 07:05:01

标签: javascript android firebase firebase-realtime-database

所以我实际上正在使用这两种方法来在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();
            }

        }
    }
}

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

1 个答案:

答案 0 :(得分: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)));
    }

error images for getContext()

error images for this, bmp