我正在为parse.com开发一个Android应用程序,我正在从parse.com获取图像文件。我想提出一个条件,如果图像文件在服务器上不可用,那么它不会使应用程序崩溃,它应该提供一个Toast消息"图像文件不可用"或者它将显示默认图像文件。
我的代码如下:
public static ParseUser user = getCurrentUser();
progressDialog = ProgressDialog.show(UserInfoActivity.this, "","Downloading Image...", true);
ParseFile fileObject = (ParseFile) user.get("profileImage");
fileObject.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Log.d("test", "We've got data in data.");
// Decode the Byte[] into Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,data.length);
Bitmap circleBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(bmp.getWidth() / 2, bmp.getHeight() / 2,
bmp.getWidth() / 2, paint);
// Get the ImageView from main.xml
ImageView image = (ImageView) findViewById(R.id.image);
// Set the Bitmap into the ImageView
image.setImageBitmap(circleBitmap);
// Close progress dialog
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "asfdsasa", Toast.LENGTH_LONG).show();
Log.d("test","There was a problem downloading the data.");
}
}
});
答案 0 :(得分:1)
来自Android API for ParseObject https://parse.com/docs/android/api/com/parse/ParseObject.html
有(String键) 此对象是否具有特定键。
所以,你应该能够:
if (getCurrentUser().has("profileImage")) {
// fetch and show
} else {
// use default image
}
您可能需要在使用user.fetchIfNeededInBackground()
之后,您可以致电getCurrentUser().get("profileImage")
获取数据。无需进行任何其他调用,因为用户对象已经填充了配置文件图像中的数据。
<强>加成:强>
看起来好像要在圆形画布上绘制图像。
Android Bootstrap库有方法可以做到这一点,这可能很有用:https://github.com/Bearded-Hen/Android-Bootstrap