1 package jp.sourceforge.gokigen.memoma.fileio;
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;
17 import jp.sourceforge.gokigen.memoma.Main;
18 import jp.sourceforge.gokigen.memoma.R;
26 public class ImageLoader
28 ProgressDialog loadingDialog;
32 String imageFile = null;
33 Bitmap imageBitmap = null;
36 ImageView imageView = null;
38 public ImageLoader(Context context)
40 loadingDialog = new ProgressDialog(context);
46 * (一度も動かしたことのないコードなので注意!)
48 * @return イメージファイル名の一覧
51 public static String[] getImageFileList(Activity activity)
55 HashSet<String> list = new HashSet<String>();
57 Cursor c = activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
60 String imagefile = c.getString(c.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
61 File file = new File(imagefile);
62 list.add(file.getParent());
64 return (String[]) list.toArray(new String[list.size()]);
78 public static Uri parseUri(String imageFile)
80 if (imageFile.startsWith("content://"))
82 return (Uri.parse(imageFile));
84 File picFile = new File(imageFile);
85 return (Uri.fromFile(picFile));
92 public static void setImage(Context context, ImageView view, String imageFile)
96 int width = view.getWidth();
97 int height = view.getHeight();
98 if (imageFile.startsWith("content://"))
100 // URIから画像を設定する...OutOfMemory対策付き
101 bitmap = getBitmapFromUri(context, Uri.parse(imageFile), width, height);
105 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
106 bitmap = getBitmap(imageFile, view.getWidth(), view.getHeight());
108 view.setScaleType(ImageView.ScaleType.FIT_XY);
109 view.setImageBitmap(bitmap);
113 * 画面にイメージを表示する (ロード中ダイアログ表示つき)
116 public void setImage(ImageView view, String targetFile)
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();
126 imageFile = targetFile;
129 imageWidth = view.getWidth();
130 imageHeight = view.getHeight();
133 Thread thread = new Thread(new Runnable()
139 if (imageFile.startsWith("content://"))
141 // URIから画像を設定する...OutOfMemory対策付き
142 imageBitmap = getBitmapFromUri(parent, Uri.parse(imageFile), imageWidth, imageHeight);
146 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
147 imageBitmap = getBitmap(imageFile, imageWidth, imageHeight);
149 handler.sendEmptyMessage(0);
153 handler.sendEmptyMessage(0);
154 ex.printStackTrace();
161 private final Handler handler = new Handler()
164 public void handleMessage(Message msg)
166 if ((imageBitmap != null)&&(imageView != null))
168 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
169 imageView.setImageBitmap(imageBitmap);
177 loadingDialog.dismiss();
187 ex.printStackTrace();
192 * URI経由でビットマップデータを取得する
195 public static Bitmap getBitmapFromUri(Context context, Uri uri, int width, int height)
197 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
198 BitmapFactory.Options opt = new BitmapFactory.Options();
200 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
201 opt.inJustDecodeBounds = true;
203 opt.inPurgeable = true;
204 opt.inPreferredConfig = Bitmap.Config.RGB_565;
206 InputStream input = null;
209 if (Build.VERSION.SDK_INT >= 19)
211 context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
213 input = context.getContentResolver().openInputStream(uri);
216 BitmapFactory.decodeStream(input, null, opt);
222 Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString() + " URI : " + uri);
223 ex.printStackTrace();
237 // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
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;
256 Bitmap retBitmap = null;
259 input = context.getContentResolver().openInputStream(uri);
260 retBitmap = BitmapFactory.decodeStream(input, null, opt);
265 Log.v(Main.APP_IDENTIFIER, "Ex(2): " + ex.toString());
278 ex.printStackTrace();
288 public static Bitmap getBitmap(String pictureString, int width, int height)
291 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
292 BitmapFactory.Options opt = new BitmapFactory.Options();
294 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
295 opt.inJustDecodeBounds = true;
297 BitmapFactory.decodeFile(pictureString, opt);
299 // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
309 // 画像の縮小サイズを決定する (縦幅、横幅の小さいほうにあわせる)
310 int widthBounds = opt.outWidth / width;
311 int heightBounds = opt.outHeight / height;
312 opt.inSampleSize=Math.min(widthBounds, heightBounds);
313 opt.inJustDecodeBounds = false;
316 return (BitmapFactory.decodeFile(pictureString, opt));