OSDN Git Service

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