我正在尝试在我的drawable文件夹中取一个png,检索它,并使用Backendless api将其作为drawable上传到我的服务器。但是,它不喜欢这样,告诉我它没有任何属性就无法更新对象。
I/System.out: fault!
I/System.out: BackendlessFault{ code: '1001', message: 'Cannot update object without any properties: image' }
守则:
try {
Event ne = new Event();
ne.title = "title of event";
ne.desc = "short desc";
ne.extDesc = "long desc";
ne.image = getDrawable(R.drawable.palestra);
System.out.println((ne.image != null ? "does" : "does not ") + "work");
Backendless.Persistence.save(ne, new AsyncCallback<Event>() {
@Override
public void handleResponse(Event event) {
System.out.println("successfull!");
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
System.out.println("fault!");
System.out.println(backendlessFault.toString());
}
});
} catch (Exception e) {
System.out.println("caught exception! " + e.toString());
}
这样做的目的是将一个示例事件发布到服务器,这样当我使用属性title,desc,extDesc和image拉动Event时,我可以相应地更新我的屏幕事件。但是,我似乎无法拍摄本地的图像并将其作为Drawable上传。
感谢您的帮助。
答案 0 :(得分:1)
一种解决方案是获取图像位图并将其保存到无尽的文件中:
//The outer Backendless call saves the photo in the file and the inner Backendless
//call saves the object to the table and gives ne.image a String reference
//to the file path. the column type should be File Reference, saving the path
//as a String willw ork
Backendless.Files.Android.upload(mBitmap,
Bitmap.CompressFormat.JPEG, //compression type
100, //quality of image
"mImageName", //What you want to call your file, eg timestamp + "app_image"
"/my_images", //The path of the file in Backendless
new AsyncCallback<BackendlessFile>() {
@Override
public void handleResponse(BackendlessFile response) {
ne.title = "title of event";
ne.desc = "short desc";
ne.extDesc = "long desc";
ne.image = (response.getFileURL());
Backendless.Persistence.of(Ids.class).save(ne, new AsyncCallback<Ids>() {
@Override
public void handleResponse(Ids response) {
}//end handleResponse
@Override
public void handleFault(BackendlessFault fault) {
}//end handleFault
});
}//end handleResponse
@Override
public void handleFault(BackendlessFault fault) {
}//end handleFault
});
要从保存的网址中检索图片,必须使用AsyncTask从网址中获取图片。
您可以创建以下类:
public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
ImageView ivMyImage;
private Bitmap myImage;
public MyAsyncTask(ImageView ivMyImage) {
this.ivMyImage = ivMyImage;
}//end constructor
@Override
protected Bitmap doInBackground(String... urlString) {
try {
URL myImageUrl = new URL(urlString[0]);
myImage = BitmapFactory.decodeStream(myImageUrl.openConnection().getInputStream());
} //end try
catch (Exception e) {
e.printStackTrace();
}//end catch
return myImage;
}//end doInBackground
@Override
protected void onPostExecute(Bitmap bitmap) {
ivMyImage.setImageBitmap(myImage);
}//end onPostExecute
}//end class
并使用以下代码在要使用异步任务的任何地方调用它:
MyAsyncTask myAsyncTask = new MyAsyncTask(ivMyImage);
myAsyncTask.execute(ne.getImage());//ne.getImage() should retrieve the image url you saved
希望这会有所帮助