往bomb上传图片至数据库失败,提示"数据上传失败107 invalid file: filename empty."

往bomb上传图片至数据库失败,提示”数据上传失败107 invalid file: filename empty.”

今天在做Android开发时,准备将图片上传至bomb并存入数据库中,结果出现”数据上传失败107 invalid file: filename empty.”
解决方法如下:
之所以会出现”数据上传失败107 invalid file: filename empty.”
是因为原因是,在Bmob中,必须先把文件上传到Bmob的后台文件管理中才能再添加到后台的数据库的表中。否则会报错。

//类的设计
public class Product extends BmobObject {
    private String name;
    private double price;
    private String information;
    private BmobFile img;
    private boolean isEmpty = false;

    public Product(){}
    public Product(String name,double price,String information)
    {
        this.name=name;
        this.price=price;
        this.information=information;
    }
    public Product(String name,double price,String information,BmobFile img)
    {
        this.name=name;
        this.price=price;
        this.information=information;
        this.img = img;
    }
    public String getName(){
        return name;
    }
    public double getPrice(){
        return price;
    }
    public String getInformation(){
        return information;
    }
    public BmobFile getImg() {return img;}
    public boolean getIsEmpty() {return isEmpty;}

    public void setImg(BmobFile img) { this.img = img; }
    public void setName(String name) { this.name = name; }
    public void setPrice(double price) { this.price = price; }
    public void setInformation(String information) { this.information = information; }
    public void setIsEmpty() { isEmpty = true; }
}
private Product p2 = new Product();
//用uploadblock将图片上传至服务器
p2.getImg().uploadblock(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "上传成功", Toast.LENGTH_SHORT).show();

                //将用户输入的信息进行读取
                            EditText pro_Name=(EditText) findViewById(R.id.SpName_EditText);
                            EditText pro_price=(EditText) findViewById(R.id.SpPrice_EditText) ;
                            EditText pro_Inf=(EditText) findViewById(R.id.SpInf_editText) ;
                double price=Double.valueOf(pro_price.getText().toString()).doubleValue();
                //检测输入是否为空 
                if(TextUtils.isEmpty(pro_Name.getText())) { 
                    Toast.makeText(getApplicationContext(), "名字为空,发布失败", Toast.LENGTH_SHORT).show(); return; 
                } 
                if(TextUtils.isEmpty(pro_price.getText())) { 
                Toast.makeText(getApplicationContext(), "单价为空,发布失败", Toast.LENGTH_SHORT).show(); return; 
                }
                //将用户输入的信息存入p2,准备存入数据库 
                p2.setName(pro_Name.getText().toString()); 
                p2.setPrice(price); 
                p2.setInformation(pro_Inf.getText().toString());
                //将p2整个对象存入数据库
                            p2.save(new SaveListener() {
                                @Override
                                public void done(String objectId,BmobException e) {
                                    if(e==null){
                                        Toast.makeText(getApplicationContext(),"添加数据成功,返回objectId为:"+objectId,Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(getApplicationContext(),"创建数据失败:" + e.getMessage(),Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });


                        }
                        else {
                            Toast.makeText(getApplicationContext(), "上传失败", Toast.LENGTH_SHORT).show();
                        }
                    }
});

有关图片的上传部分,记得一定要将存入数据库的部分代码放在上传图片的函数里。
也就是

bombfile.uploadblock(new UploadFileListener() {
    @Override
        public void done(BmobException e) {
            if (e == null) {            
            p2.save(new SaveListener() {
                                @Override
                                public void done(String objectId,BmobException e) {
                                    if(e==null){
                                        Toast.makeText(getApplicationContext(),"添加数据成功,返回objectId为:"+objectId,Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(getApplicationContext(),"创建数据失败:" + e.getMessage(),Toast.LENGTH_SHORT).show();
                                    }
                                }
                        });
        }
        else{
        }
)};

如果将存入数据库的部分代码放在上传函数的部分下面的话也会提示”数据上传失败107 invalid file: filename empty.”关于图片地址解析部分,下篇博客再来介绍

你可能感兴趣的:(Android)