我正在尝试在可绘制的文件夹中使用PNG,检索并将其上传到我的服务器上,作为使用Backendless API绘制的可绘制。但是,它不喜欢这样,告诉我没有任何属性就无法更新对象。
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());
}
目的是向服务器发布一个示例事件,以便当我将事件拉动属性标题,desc,extdesc和image时,我可以相应地更新屏幕上的事件。但是,我似乎无法拍摄我本地拥有的图像,并将其作为可绘制的图像上传。
感谢您的任何帮助。
一种解决方案是获取图像位图并将其保存到后端的文件中:
//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
});
要从您将其保存到的URL上检索图像,您必须使用异步来从URL获取图像。
您可以创建以下类:
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
希望这有帮助