啊哈,Day 10,终于来到了两位数啦,我发现码字比码代码难多了,写了好多代码要想整理成通顺的文字都不太容易,越是困难的事情就是越要去做,这些都是自己的短板嘛,既然发现了为什么不去改变呢?改变了不就是进步了吗,终身学习说的不就是这个吗。好了,回到我们这个项目吧,现在客户端已经能查询服务器保存的照片了,不过页面还是比较粗糙,接下来还要加按日期查询的功能,现在也想不出来还需要加什么功能了,而且也不太擅长安卓前端,效果自己也不太满意,但是还是要坚持到这个项目做完整,先看下截图吧。
ServerAlbumViewActivity.java
package cn.cloudfk.taoalbum.activity;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ImageView;
import cn.cloudfk.taoalbum.R;
import cn.cloudfk.taoalbum.callback.ServiceHelper;
import cn.cloudfk.taoalbum.common.IntentUtil;
import cn.cloudfk.taoalbum.data.dto.ResultData;
public class ServerAlbumViewActivity extends AppCompatActivity implements ServiceHelper.ServiceCallback{
private ImageView imageview;
private Bitmap bitmap =null;
String fileName;
private static final String TAG = "ServerAlbumViewActivity";
private Toolbar mToolbar;
ServiceHelper.ServiceCallback callback;
Activity context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_album_view);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
fileName = getIntent().getStringExtra(IntentUtil.FILENAME_KEY);
callback = this;
context = this;
//server_image_view
imageview = findViewById(R.id.server_image_view);
Log.i(TAG,"fileName="+fileName);
new Thread(new Runnable() {
@Override
public void run() {
ServiceHelper.showPhoto(fileName,callback);
}
}).start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home: {
finish();
break;
}
}
return true;
}
@Override
public void onSuccess(String service, ResultData ret) {
if (ServiceHelper.SHOW_PHOTO_KEY.equals(service) && ret!=null
&& ret.isSuccess() && ret.getData()!=null) {
bitmap = (Bitmap)ret.getData();
context.runOnUiThread(new Runnable() {
@Override
public void run() {
imageview.setImageBitmap(bitmap);
imageview.setMaxHeight(bitmap.getHeight());
imageview.setMaxWidth(bitmap.getWidth());
Log.i(TAG,"height="+bitmap.getHeight());
Log.i(TAG,"width="+bitmap.getWidth());
}
});
}
}
@Override
public void onFailure(String service, ResultData ret) {
Log.i(TAG,"service="+service+".onFailure");
}
}
ServiceHelper.java
package cn.cloudfk.taoalbum.callback;
import android.graphics.Bitmap;
import android.util.Log;
import cn.cloudfk.taoalbum.data.GlobalData;
import cn.cloudfk.taoalbum.data.dto.ResultData;
import cn.cloudfk.taoalbum.utils.HttpAssist;
import cn.cloudfk.taoalbum.utils.KitJson;
public class ServiceHelper {
private static final String TAG = "ServiceHelper";
public static final String PHOTO_LIST_KEY = "photoList";
public static final String SHOW_PHOTO_KEY= "showPhoto";
public interface ServiceCallback{
void onSuccess(String service, ResultData ret);
void onFailure(String service, ResultData ret);
}
public static void photoList( final ServiceCallback callback){
Log.i(TAG,"call photoList.");
String content = HttpAssist.getInstance().getImgList();
if (content!=null){
Log.i(TAG,"content.length="+content.length());
ResultData ret = KitJson.toResultData(content);
if (ret!=null){
Log.i(TAG,"ret.state="+ret.getState());
if(ret.isSuccess()){
callback.onSuccess(PHOTO_LIST_KEY,ret);
return;
}
}
callback.onFailure(PHOTO_LIST_KEY,ret);
}
}
public static void showPhoto(String fileName,final ServiceCallback callback){
String imgUrl = GlobalData.param.getServerUrl()+"/show/"+fileName;
Bitmap bitmap = HttpAssist.getInstance().getBitmap(imgUrl);
ResultData ret = new ResultData();
ret.setState(ResultData.FAIL);
if (bitmap!=null){
ret.setState(ResultData.SUCCESS);
ret.setData(bitmap);
callback.onSuccess(SHOW_PHOTO_KEY,ret);
}
callback.onFailure(SHOW_PHOTO_KEY,ret);
}
}