来自图库的图片未显示在图片按钮

时间:2018-02-09 16:38:45

标签: java android firebase firebase-storage

从应用程序我应该可以将图库中的图像添加到图像按钮,然后添加食物名称,描述和价格,最后点击上传。 图像应上传到我的firebase存储,而Price,Description和Food名称应上传到firebase数据库

StorageReference filePath = storageReference.child(uri.getLastPathSegment());会导致错误,因此图片不会显示在图片按钮上,也不会上传到firebase存储空间。

public class AddFood extends AppCompatActivity {

private ImageButton foodImage;
private static final int GALLREQ=1;
private EditText name,desc,price;
private Uri uri = null;
private StorageReference storageReference = null;
private DatabaseReference mRef;
private FirebaseDatabase firebaseDatabase;



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

    name = (EditText) findViewById(R.id.ItemName);
    desc = (EditText) findViewById(R.id.ItemDesc);
    price = (EditText) findViewById(R.id.ItemPrice);
    storageReference=  FirebaseStorage.getInstance().getReference();
    mRef= FirebaseDatabase.getInstance().getReference("Item");

}


public void ImageButtonclicked(View view){
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(galleryIntent,GALLREQ);


}


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

    if(resultCode==GALLREQ&& resultCode==RESULT_OK) {


            uri = data.getData();
            foodImage = (ImageButton) findViewById(R.id.FoodImageButton);
            foodImage.setImageURI(uri);

    }


}

public void uploadClicked(View view){
   final String name_text = name.getText().toString().trim();
   final String desc_text = desc.getText().toString().trim();
    String price_text = price.getText().toString().trim();

    if (!TextUtils.isEmpty(name_text)&&!TextUtils.isEmpty(desc_text)&&!TextUtils.isEmpty(price_text)){

        StorageReference filePath = storageReference.child(uri.getLastPathSegment());
        filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                final Uri downloaduri = taskSnapshot.getDownloadUrl();
                Toast.makeText(AddFood.this,"uploaded",Toast.LENGTH_LONG).show();
            }
        });


        final  DatabaseReference newPost = mRef.push();
        newPost.child("name").setValue(name_text);
        newPost.child("desc").setValue(desc);
        newPost.child("price").setValue(price_text);


    }

}

}

0 个答案:

没有答案