OSDN Git Service

8a71e95485a65bce041fc2b2b458a91357fa27ce
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / ImageLoader.java
1 package jp.sourceforge.gokigen.memoma;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import android.app.ProgressDialog;
6 import android.content.Context;
7 import android.graphics.Bitmap;
8 import android.graphics.BitmapFactory;
9 import android.net.Uri;
10 import android.os.Handler;
11 import android.os.Message;
12 import android.util.Log;
13 import android.widget.ImageView;
14
15 /**
16  *  画像イメージを読み込む
17  * 
18  * @author MRSa
19  *
20  */
21 public class ImageLoader
22 {
23     ProgressDialog loadingDialog = null;
24     Context  parent = null;
25
26     // 画像を表示する
27     String imageFile = null;
28         Bitmap imageBitmap = null;
29         int imageWidth     = 1;
30         int imageHeight    = 1;
31         ImageView imageView = null;
32     
33     public ImageLoader(Context context)
34     {
35         loadingDialog = new ProgressDialog(context);
36         parent = context;
37     }
38     
39     /**
40      *  イメージファイルの一覧を取得する
41      *  (一度も動かしたことのないコードなので注意!)
42      * @param activity
43      * @return イメージファイル名の一覧
44      */
45     /*
46     public static String[] getImageFileList(Activity activity)
47     {
48         try
49         {
50                 HashSet<String> list = new HashSet<String>();
51                 
52                 Cursor c = activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
53                 while(c.moveToNext())
54                 {
55                 String imagefile = c.getString(c.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
56                 File file = new File(imagefile);
57                 list.add(file.getParent());
58                 }
59             return (String[]) list.toArray(new String[list.size()]);
60         }
61         catch (Exception ex)
62         {
63             //
64         }
65         return (null);
66     }
67     */
68
69     /**
70          *  URIから変換
71          * 
72          * @param imageFile
73          * @return
74          */
75         public static Uri parseUri(String imageFile)
76         {
77             if (imageFile.startsWith("content://") == true)
78             {
79                 return (Uri.parse(imageFile));
80             }
81
82         File picFile = new File(imageFile);
83         return (Uri.fromFile(picFile)); 
84         }
85
86         /**
87          *   画面にイメージを表示する 
88          *
89          */
90         public static void setImage(Context context, ImageView view, String imageFile)
91     {
92         // 画像を表示する
93                 Bitmap bitmap = null;
94                 int width = view.getWidth();
95                 int height = view.getHeight();
96         if (imageFile.startsWith("content://") == true)
97         {
98                 // URIから画像を設定する...OutOfMemory対策付き
99                 bitmap = getBitmapFromUri(context, Uri.parse(imageFile), width, height);
100         }
101         else
102         {
103                 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
104                 bitmap = getBitmap(imageFile, view.getWidth(), view.getHeight());
105         }
106         view.setScaleType(ImageView.ScaleType.FIT_XY);
107         view.setImageBitmap(bitmap);
108     }
109
110         /**
111          *   画面にイメージを表示する (ロード中ダイアログ表示つき)
112          * 
113          */
114         public void setImage(ImageView view, String targetFile)
115     {
116
117         //  プログレスダイアログ(「ロード中...」)を表示する。
118         loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
119         loadingDialog.setMessage(parent.getString(R.string.dataLoading));
120         loadingDialog.setIndeterminate(true);
121         loadingDialog.setCancelable(false);
122         loadingDialog.show();
123
124         imageFile = targetFile;
125         imageView = view;
126         imageBitmap = null;
127         imageWidth = view.getWidth();
128         imageHeight = view.getHeight();        
129         
130         /**
131          *  ダイアログ表示中の処理
132          * 
133          */
134         Thread thread = new Thread(new Runnable()
135         {  
136             public void run()
137             {
138                 try
139                 {                       
140                         if (imageFile.startsWith("content://") == true)
141                     {
142                         // URIから画像を設定する...OutOfMemory対策付き
143                         imageBitmap = getBitmapFromUri(parent, Uri.parse(imageFile), imageWidth, imageHeight);
144                     }
145                     else
146                     {
147                         // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
148                         imageBitmap = getBitmap(imageFile, imageWidth, imageHeight);
149                     }
150                         handler.sendEmptyMessage(0);
151                 }
152                 catch (Exception ex)
153                 {
154                         handler.sendEmptyMessage(0);
155                 }
156             }
157
158             /**
159              *   画面の更新
160              */
161             private final Handler handler = new Handler()
162             {
163                 @Override
164                 public void handleMessage(Message msg)
165                 {
166                         if ((imageBitmap != null)&&(imageView != null))
167                         {
168                         imageView.setScaleType(ImageView.ScaleType.FIT_XY);
169                         imageView.setImageBitmap(imageBitmap);
170                         }
171                     imageFile = null;
172                     imageView = null;
173                     imageBitmap = null;
174                     imageWidth = 1;
175                     imageHeight = 1;
176
177                         loadingDialog.dismiss();
178                 
179                 }
180             };   
181         });
182         try
183         {
184             thread.start();
185         }
186         catch (Exception ex)
187         {
188
189         }
190     }
191         
192         /**
193          *   URI経由でビットマップデータを取得する
194          * 
195          * @param context
196          * @param uri
197          * @param width
198          * @param height
199          * @return
200          */
201     public static Bitmap getBitmapFromUri(Context context, Uri uri, int width, int height)
202     {
203         // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
204         BitmapFactory.Options opt = new BitmapFactory.Options();
205
206         // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
207         opt.inJustDecodeBounds = true;
208         opt.inDither = true;
209         opt.inPurgeable = true;
210         opt.inPreferredConfig = Bitmap.Config.RGB_565;
211
212         InputStream input = null; 
213         try
214         { 
215             input = context.getContentResolver().openInputStream(uri); 
216             BitmapFactory.decodeStream(input, null, opt); 
217             input.close();
218         }
219         catch (Exception ex)
220         {
221                 Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString());
222                 if (input != null)
223                 {
224                         try
225                         {
226                         input.close();
227                         }
228                         catch (Exception e)
229                         {
230                                 //
231                         }
232                 }
233         }
234         // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
235         if (width < 10)
236         {
237             width = 320;
238         }
239         if (height < 10)
240         {
241                 height = 240;
242         }
243
244         // 画像の縮小サイズを決定する (縦幅、横幅の小さいほうにあわせる)
245         int widthBounds = opt.outWidth / width;
246         int heightBounds = opt.outHeight / height;
247         opt.inSampleSize=Math.min(widthBounds, heightBounds);
248         opt.inJustDecodeBounds = false;
249         opt.inPreferredConfig = Bitmap.Config.RGB_565;
250         
251         // 画像ファイルを応答する
252         input = null; 
253         Bitmap retBitmap = null;
254         try
255         { 
256             input = context.getContentResolver().openInputStream(uri); 
257             retBitmap = BitmapFactory.decodeStream(input, null, opt); 
258             input.close();
259         }
260         catch (Exception ex)
261         {
262                 Log.v(Main.APP_IDENTIFIER, "Ex(2): " + ex.toString());
263                 if (input != null)
264                 {
265                         try
266                         {
267                         input.close();
268                         }
269                         catch (Exception e)
270                         {
271                                 //
272                         }
273                 }
274         }
275         return (retBitmap);
276     }        
277     
278     /**
279      *   ビットマップデータを取得する
280      * 
281      * @param pictureString
282      * @param width
283      * @param height
284      * @return ビットマップデータ
285      */
286     public static Bitmap getBitmap(String pictureString, int width, int height)
287     {
288
289         // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
290         BitmapFactory.Options opt = new BitmapFactory.Options();
291
292         // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
293         opt.inJustDecodeBounds = true;
294         opt.inDither = true;
295         BitmapFactory.decodeFile(pictureString, opt);
296
297         // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
298         if (width < 10)
299         {
300             width = 320;
301         }
302         if (height < 10)
303         {
304                 height = 240;
305         }
306
307         // 画像の縮小サイズを決定する (縦幅、横幅の小さいほうにあわせる)
308         int widthBounds = opt.outWidth / width;
309         int heightBounds = opt.outHeight / height;
310         opt.inSampleSize=Math.min(widthBounds, heightBounds);
311         opt.inJustDecodeBounds = false;
312         
313         // 画像ファイルを応答する
314         return (BitmapFactory.decodeFile(pictureString, opt));
315     }    
316 }