OSDN Git Service

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