@@ -16,6 +16,7 @@ |
||
16 | 16 |
<uses-feature android:name="android.hardware.camera.autofocus" /> |
17 | 17 |
|
18 | 18 |
<application |
19 |
+ android:name=".App" |
|
19 | 20 |
android:allowBackup="true" |
20 | 21 |
android:icon="@drawable/ic_launcher" |
21 | 22 |
android:label="@string/app_name" |
@@ -0,0 +1,60 @@ |
||
1 |
+package ai.pai.lensman; |
|
2 |
+ |
|
3 |
+import android.app.Application; |
|
4 |
+ |
|
5 |
+import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; |
|
6 |
+import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; |
|
7 |
+import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache; |
|
8 |
+import com.nostra13.universalimageloader.core.ImageLoader; |
|
9 |
+import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; |
|
10 |
+import com.nostra13.universalimageloader.core.assist.QueueProcessingType; |
|
11 |
+import com.nostra13.universalimageloader.utils.StorageUtils; |
|
12 |
+ |
|
13 |
+import ai.pai.lensman.utils.Constants; |
|
14 |
+ |
|
15 |
+/** |
|
16 |
+ * Created by sky on 2015/7/6. |
|
17 |
+ */ |
|
18 |
+public class App extends Application { |
|
19 |
+ |
|
20 |
+ protected static App mInstance; |
|
21 |
+ |
|
22 |
+ public App(){ |
|
23 |
+ mInstance = this; |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ public static App getApp() { |
|
27 |
+ if (mInstance != null && mInstance instanceof App) { |
|
28 |
+ return (App) mInstance; |
|
29 |
+ } else { |
|
30 |
+ mInstance = new App(); |
|
31 |
+ mInstance.onCreate(); |
|
32 |
+ return (App) mInstance; |
|
33 |
+ } |
|
34 |
+ } |
|
35 |
+ |
|
36 |
+ @Override |
|
37 |
+ public void onCreate() { |
|
38 |
+ super.onCreate(); |
|
39 |
+ initImageLoader(); |
|
40 |
+ mInstance = this; |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ |
|
44 |
+ private void initImageLoader() { |
|
45 |
+ |
|
46 |
+ ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) |
|
47 |
+ .memoryCacheExtraOptions(320, 320) |
|
48 |
+ .diskCacheExtraOptions(320, 320, null) |
|
49 |
+ .threadPriority(Thread.NORM_PRIORITY - 2) |
|
50 |
+ .denyCacheImageMultipleSizesInMemory() |
|
51 |
+ .diskCacheFileNameGenerator(new Md5FileNameGenerator()) |
|
52 |
+ .diskCache(new UnlimitedDiskCache(StorageUtils.getOwnCacheDirectory(this, Constants.TMP_DIR_NAME))) |
|
53 |
+ .diskCacheSize(50 * 1024 * 1024).tasksProcessingOrder(QueueProcessingType.LIFO) |
|
54 |
+ .memoryCache(new WeakMemoryCache()).memoryCacheSizePercentage(20) |
|
55 |
+ .threadPoolSize(4) |
|
56 |
+ .build(); |
|
57 |
+ ImageLoader.getInstance().init(config); |
|
58 |
+ } |
|
59 |
+ |
|
60 |
+} |
@@ -10,7 +10,7 @@ public final class BoxUrlContainer { |
||
10 | 10 |
|
11 | 11 |
public static final String FETCH_THUMBNAIL_URL = BASE_URL+"fetch_thumbnail"; |
12 | 12 |
|
13 |
- public static final String FETCH_ORIGIN_URL = BASE_URL+"fetch_origin"; |
|
13 |
+ public static final String PHOTO_PATH_PREFIX_URL = BASE_URL+"static/"; |
|
14 | 14 |
|
15 | 15 |
|
16 | 16 |
} |
@@ -8,11 +8,15 @@ import android.view.ViewGroup; |
||
8 | 8 |
import android.widget.ImageView; |
9 | 9 |
|
10 | 10 |
import com.android.common.utils.DeviceUtils; |
11 |
+import com.nostra13.universalimageloader.core.DisplayImageOptions; |
|
11 | 12 |
|
13 |
+import java.io.File; |
|
12 | 14 |
import java.util.ArrayList; |
13 | 15 |
|
14 | 16 |
import ai.pai.lensman.R; |
15 | 17 |
import ai.pai.lensman.bean.PhotoBean; |
18 |
+import ai.pai.lensman.utils.Constants; |
|
19 |
+import ai.pai.lensman.utils.ImageLoaderUtils; |
|
16 | 20 |
import butterknife.BindView; |
17 | 21 |
import butterknife.ButterKnife; |
18 | 22 |
|
@@ -22,11 +26,13 @@ public class PhotoRecyclerAdapter extends RecyclerView.Adapter<PhotoRecyclerAdap |
||
22 | 26 |
private LayoutInflater mInflater; |
23 | 27 |
private ArrayList<PhotoBean> photoList; |
24 | 28 |
private int width; |
29 |
+ private DisplayImageOptions options; |
|
25 | 30 |
|
26 | 31 |
public PhotoRecyclerAdapter(Context context){ |
27 | 32 |
this.context = context; |
28 | 33 |
width = DeviceUtils.getScreenWidth(this.context); |
29 | 34 |
mInflater = LayoutInflater.from(this.context); |
35 |
+ options = ImageLoaderUtils.getOptions(R.drawable.default_img); |
|
30 | 36 |
} |
31 | 37 |
|
32 | 38 |
public synchronized void addPhotoBean(PhotoBean item){ |
@@ -67,7 +73,8 @@ public class PhotoRecyclerAdapter extends RecyclerView.Adapter<PhotoRecyclerAdap |
||
67 | 73 |
return; |
68 | 74 |
} |
69 | 75 |
final PhotoBean item = photoList.get(position); |
70 |
- holder.photo.setImageResource(R.drawable.demo1); |
|
76 |
+ String path = Constants.APP_IMAGE_DIR + File.separator+item.sessionId+File.separator+Constants.THUMBNAIL_DIR_NAME+File.separator+item.photoName; |
|
77 |
+ ImageLoaderUtils.displayLocalImage(path, holder.photo, options); |
|
71 | 78 |
int height = width*10/16; |
72 | 79 |
ViewGroup.LayoutParams lp=holder.photo.getLayoutParams(); |
73 | 80 |
lp.width = width; |
@@ -8,6 +8,11 @@ import com.android.common.utils.LogHelper; |
||
8 | 8 |
import org.json.JSONArray; |
9 | 9 |
import org.json.JSONObject; |
10 | 10 |
|
11 |
+import java.io.File; |
|
12 |
+import java.io.FileOutputStream; |
|
13 |
+import java.io.InputStream; |
|
14 |
+import java.net.HttpURLConnection; |
|
15 |
+import java.net.URL; |
|
11 | 16 |
import java.util.ArrayList; |
12 | 17 |
import java.util.HashMap; |
13 | 18 |
import java.util.Timer; |
@@ -16,6 +21,7 @@ import java.util.TimerTask; |
||
16 | 21 |
import ai.pai.lensman.bean.PhotoBean; |
17 | 22 |
import ai.pai.lensman.bean.SessionBean; |
18 | 23 |
import ai.pai.lensman.box.BoxUrlContainer; |
24 |
+import ai.pai.lensman.utils.Constants; |
|
19 | 25 |
import ai.pai.lensman.utils.HttpPostTask; |
20 | 26 |
|
21 | 27 |
public class SessionInteractor { |
@@ -37,7 +43,7 @@ public class SessionInteractor { |
||
37 | 43 |
|
38 | 44 |
void onSessionStartSuccess(String session); |
39 | 45 |
void onSessionStartError(String session); |
40 |
- void onSessionPhotoCaptured(ArrayList<PhotoBean> bean); |
|
46 |
+ void onSessionPhotoCaptured(PhotoBean bean); |
|
41 | 47 |
void onSessionEnd(String session); |
42 | 48 |
} |
43 | 49 |
|
@@ -140,12 +146,78 @@ public class SessionInteractor { |
||
140 | 146 |
@Override |
141 | 147 |
protected void onPostSuccess() { |
142 | 148 |
super.onPostSuccess(); |
143 |
- listener.onSessionPhotoCaptured(photoList); |
|
149 |
+ for(PhotoBean bean :photoList){ |
|
150 |
+ new SavePhotoTask(bean).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(),bean); |
|
151 |
+ } |
|
144 | 152 |
} |
145 | 153 |
}; |
146 | 154 |
fetchThumbnailTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), BoxUrlContainer.FETCH_THUMBNAIL_URL); |
147 | 155 |
} |
148 | 156 |
|
157 |
+ |
|
158 |
+ class SavePhotoTask extends AsyncTask<PhotoBean, Integer, Boolean> { |
|
159 |
+ |
|
160 |
+ private PhotoBean photoBean; |
|
161 |
+ public SavePhotoTask(PhotoBean bean){ |
|
162 |
+ this.photoBean = bean; |
|
163 |
+ } |
|
164 |
+ |
|
165 |
+ @Override |
|
166 |
+ protected Boolean doInBackground(PhotoBean... params) { |
|
167 |
+ PhotoBean photoBean = params[0]; |
|
168 |
+ String path = BoxUrlContainer.PHOTO_PATH_PREFIX_URL+photoBean.photoPath; |
|
169 |
+ LogHelper.d(TAG,"保存照片到本地,图片链接地址为"+path); |
|
170 |
+ FileOutputStream fOut = null; |
|
171 |
+ HttpURLConnection conn = null; |
|
172 |
+ InputStream inStream = null; |
|
173 |
+ try { |
|
174 |
+ URL url = new URL(path); |
|
175 |
+ conn = (HttpURLConnection) url.openConnection(); |
|
176 |
+ conn.setConnectTimeout(5 * 1000); |
|
177 |
+ conn.setRequestMethod("GET"); |
|
178 |
+ inStream = conn.getInputStream(); |
|
179 |
+ if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ |
|
180 |
+ String dirPath = Constants.APP_IMAGE_DIR +File.separator+photoBean.sessionId+File.separator+Constants.THUMBNAIL_DIR_NAME; |
|
181 |
+ File dir = new File(dirPath); |
|
182 |
+ dir.mkdirs(); |
|
183 |
+ File file = new File(dir, photoBean.photoName); |
|
184 |
+ fOut = new FileOutputStream(file); |
|
185 |
+ byte[] buffer = new byte[2048]; |
|
186 |
+ int len ; |
|
187 |
+ while((len=inStream.read(buffer))!=-1){ |
|
188 |
+ fOut.write(buffer,0,len); |
|
189 |
+ fOut.flush(); |
|
190 |
+ } |
|
191 |
+ fOut.flush(); |
|
192 |
+ LogHelper.d("czy","保存照片到本地,图片保存至"+file.getAbsolutePath()+"图片大小为"+file.length()+"字节\n\n"); |
|
193 |
+ } |
|
194 |
+ return true; |
|
195 |
+ } catch (Exception e) { |
|
196 |
+ e.printStackTrace(); |
|
197 |
+ } finally { |
|
198 |
+ try{ |
|
199 |
+ inStream.close(); |
|
200 |
+ conn.disconnect(); |
|
201 |
+ fOut.close(); |
|
202 |
+ }catch (Exception e){ |
|
203 |
+ e.printStackTrace(); |
|
204 |
+ } |
|
205 |
+ } |
|
206 |
+ return false; |
|
207 |
+ } |
|
208 |
+ |
|
209 |
+ @Override |
|
210 |
+ protected void onPostExecute(Boolean result) { |
|
211 |
+ super.onPostExecute(result); |
|
212 |
+ if (result) { |
|
213 |
+ listener.onSessionPhotoCaptured(photoBean); |
|
214 |
+ }else{ |
|
215 |
+ new SavePhotoTask(photoBean).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(),photoBean); |
|
216 |
+ } |
|
217 |
+ } |
|
218 |
+ } |
|
219 |
+ |
|
220 |
+ |
|
149 | 221 |
public void endSession(){ |
150 | 222 |
cancelTask(sessionEndTask); |
151 | 223 |
HashMap<String,String> params = new HashMap<>(); |
@@ -43,12 +43,10 @@ public class SessionPresenter implements SessionContract.Presenter,SessionIntera |
||
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
@Override |
46 |
- public void onSessionPhotoCaptured(final ArrayList<PhotoBean> beans) { |
|
46 |
+ public void onSessionPhotoCaptured(final PhotoBean bean) { |
|
47 | 47 |
sessionView.showPhotoRecyclerView(); |
48 |
- sessionView.showToast("发现新增照片"+beans.size()+"张"); |
|
49 |
- for (PhotoBean bean : beans){ |
|
50 |
- sessionView.addNewPhoto(bean); |
|
51 |
- } |
|
48 |
+ sessionView.showToast("发现新增照片"); |
|
49 |
+ sessionView.addNewPhoto(bean); |
|
52 | 50 |
} |
53 | 51 |
|
54 | 52 |
@Override |
@@ -147,7 +147,7 @@ public class UploadActivity extends BaseActivity implements UploadContract.View |
||
147 | 147 |
sessionBean.lensmanId = Preferences.getInstance(this).getLensManId(); |
148 | 148 |
sessionBean.sessionDate=20160813; |
149 | 149 |
sessionBean.sessionSeq = new Random().nextInt(10000); |
150 |
- sessionBean.sessionId ="chengzhenyu_test"; |
|
150 |
+ sessionBean.sessionId ="chengzhenyu_test"+sessionBean.sessionSeq; |
|
151 | 151 |
Intent intent = new Intent(this, SessionActivity.class); |
152 | 152 |
intent.putExtra("session",sessionBean); |
153 | 153 |
startActivity(intent); |
@@ -0,0 +1,16 @@ |
||
1 |
+package ai.pai.lensman.utils; |
|
2 |
+ |
|
3 |
+import android.os.Environment; |
|
4 |
+ |
|
5 |
+/** |
|
6 |
+ * Created by chengzhenyu on 2016/8/13. |
|
7 |
+ */ |
|
8 |
+public class Constants { |
|
9 |
+ |
|
10 |
+ public static final String APP_ROOT_DIR = Environment.getExternalStorageDirectory() + "/lensman"; |
|
11 |
+ public static final String APP_IMAGE_DIR = APP_ROOT_DIR + "/image"; |
|
12 |
+ public static final String ORIGIN_DIR_NAME = "origin"; |
|
13 |
+ public static final String THUMBNAIL_DIR_NAME = "thumbnail"; |
|
14 |
+ public static final String TMP_DIR_NAME = "tmp"; |
|
15 |
+ |
|
16 |
+} |
@@ -0,0 +1,40 @@ |
||
1 |
+package ai.pai.lensman.utils; |
|
2 |
+ |
|
3 |
+import android.graphics.Bitmap; |
|
4 |
+import android.widget.ImageView; |
|
5 |
+ |
|
6 |
+import com.nostra13.universalimageloader.core.DisplayImageOptions; |
|
7 |
+import com.nostra13.universalimageloader.core.ImageLoader; |
|
8 |
+import com.nostra13.universalimageloader.core.assist.ImageScaleType; |
|
9 |
+import com.nostra13.universalimageloader.core.imageaware.ImageViewAware; |
|
10 |
+ |
|
11 |
+public class ImageLoaderUtils { |
|
12 |
+ |
|
13 |
+ /** |
|
14 |
+ * display local image |
|
15 |
+ * @param uri |
|
16 |
+ * @param imageView |
|
17 |
+ * @param options |
|
18 |
+ */ |
|
19 |
+ public static void displayLocalImage(String uri, ImageView imageView, DisplayImageOptions options) { |
|
20 |
+ ImageLoader.getInstance().displayImage("file://" + uri, new ImageViewAware(imageView), options, null, null); |
|
21 |
+ } |
|
22 |
+ |
|
23 |
+ /** |
|
24 |
+ * display Drawable image |
|
25 |
+ * @param uri |
|
26 |
+ * @param imageView |
|
27 |
+ * @param options |
|
28 |
+ */ |
|
29 |
+ public static void displayDrawableImage(String uri, ImageView imageView, DisplayImageOptions options) { |
|
30 |
+ ImageLoader.getInstance().displayImage("drawable://" + uri, new ImageViewAware(imageView), options, null, null); |
|
31 |
+ } |
|
32 |
+ |
|
33 |
+ public static DisplayImageOptions getOptions(int drawable) { |
|
34 |
+ return new DisplayImageOptions.Builder() |
|
35 |
+ .showImageForEmptyUri(drawable).showImageOnFail(drawable) |
|
36 |
+ .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true) |
|
37 |
+ .bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).build(); |
|
38 |
+ } |
|
39 |
+ |
|
40 |
+} |