图片uri值为null,我无法弄清为什么

时间:2018-06-21 16:51:42

标签: android firebase android-fragments firebase-storage

我正在尝试捕获图像并将其存储在Firebase存储中,唯一想到的是imguri值为null。但是我不知道为什么?请帮我解决这个问题。

public class Addproduct extends Fragment{

    private ImageButton imgbtn;
    private EditText et_prod_name,et_prod_price,et_prod_quan;
    private Button btn_store_details;

    private static final int camera_req_code=1;

    private ProgressDialog pd;

    private Uri imguri=null;

    private StorageReference mstorage;

    @SuppressLint("InflateParams")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_addproduct,null);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        imgbtn=(ImageButton) view.findViewById(R.id.imageButtonprod_photo);
        et_prod_name=(EditText)view.findViewById(R.id.editTextprod_name);
        et_prod_price=(EditText)view.findViewById(R.id.editTextprod_price);
        et_prod_quan=(EditText)view.findViewById(R.id.editTextprod_quan);
        btn_store_details=(Button)view.findViewById(R.id.button_storedetails);

        pd=new ProgressDialog(getActivity());

        mstorage= FirebaseStorage.getInstance().getReference();

        imgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//capturing image
                startActivityForResult(intent,camera_req_code);
                intent.setType("image/*");
            }

        });
        btn_store_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                store_details();

            }
        });
    }

    private void store_details() {

        String prod_name=et_prod_name.getText().toString().trim();
        String prod_price=et_prod_price.getText().toString().trim();
        String prod_quan=et_prod_quan.getText().toString().trim();

        if(!TextUtils.isEmpty(prod_name) && !TextUtils.isEmpty(prod_price) && !TextUtils.isEmpty(prod_quan) && imguri!=null)
        {
            pd.setMessage("Uploading product info....");
            pd.show();
            StorageReference filepath=mstorage.child("product_images").child(imguri.getLastPathSegment());
            filepath.putFile(imguri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    //Uri downloaduri=taskSnapshot.getDownloadUri();
                    pd.dismiss();
                }
            });
        }
        else
        {
            Toast.makeText(getActivity(),"some input fields are blank",Toast.LENGTH_SHORT).show();
        }
    }

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

        if(requestCode==camera_req_code && resultCode==RESULT_OK)
        {
            imguri=data.getData();
            imgbtn.setImageURI(imguri);
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imgbtn.setImageBitmap(photo);
        }
    }
}

我能够使用捕获的位图显示图像,但是imguri为什么仍然为空?

0 个答案:

没有答案