1 package jp.sourceforge.gokigen.memoma;
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;
21 public class ImageLoader
23 ProgressDialog loadingDialog = null;
24 Context parent = null;
27 String imageFile = null;
28 Bitmap imageBitmap = null;
31 ImageView imageView = null;
33 public ImageLoader(Context context)
35 loadingDialog = new ProgressDialog(context);
41 * (一度も動かしたことのないコードなので注意!)
43 * @return イメージファイル名の一覧
46 public static String[] getImageFileList(Activity activity)
50 HashSet<String> list = new HashSet<String>();
52 Cursor c = activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
55 String imagefile = c.getString(c.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
56 File file = new File(imagefile);
57 list.add(file.getParent());
59 return (String[]) list.toArray(new String[list.size()]);
75 public static Uri parseUri(String imageFile)
77 if (imageFile.startsWith("content://") == true)
79 return (Uri.parse(imageFile));
82 File picFile = new File(imageFile);
83 return (Uri.fromFile(picFile));
90 public static void setImage(Context context, ImageView view, String imageFile)
94 int width = view.getWidth();
95 int height = view.getHeight();
96 if (imageFile.startsWith("content://") == true)
98 // URIから画像を設定する...OutOfMemory対策付き
99 bitmap = getBitmapFromUri(context, Uri.parse(imageFile), width, height);
103 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
104 bitmap = getBitmap(imageFile, view.getWidth(), view.getHeight());
106 view.setScaleType(ImageView.ScaleType.FIT_XY);
107 view.setImageBitmap(bitmap);
111 * 画面にイメージを表示する (ロード中ダイアログ表示つき)
114 public void setImage(ImageView view, String targetFile)
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();
124 imageFile = targetFile;
127 imageWidth = view.getWidth();
128 imageHeight = view.getHeight();
134 Thread thread = new Thread(new Runnable()
140 if (imageFile.startsWith("content://") == true)
142 // URIから画像を設定する...OutOfMemory対策付き
143 imageBitmap = getBitmapFromUri(parent, Uri.parse(imageFile), imageWidth, imageHeight);
147 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
148 imageBitmap = getBitmap(imageFile, imageWidth, imageHeight);
150 handler.sendEmptyMessage(0);
154 handler.sendEmptyMessage(0);
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();
193 * URI経由でビットマップデータを取得する
201 public static Bitmap getBitmapFromUri(Context context, Uri uri, int width, int height)
203 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
204 BitmapFactory.Options opt = new BitmapFactory.Options();
206 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
207 opt.inJustDecodeBounds = true;
209 opt.inPurgeable = true;
210 opt.inPreferredConfig = Bitmap.Config.RGB_565;
212 InputStream input = null;
215 input = context.getContentResolver().openInputStream(uri);
216 BitmapFactory.decodeStream(input, null, opt);
221 Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString());
234 // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
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;
253 Bitmap retBitmap = null;
256 input = context.getContentResolver().openInputStream(uri);
257 retBitmap = BitmapFactory.decodeStream(input, null, opt);
262 Log.v(Main.APP_IDENTIFIER, "Ex(2): " + ex.toString());
281 * @param pictureString
286 public static Bitmap getBitmap(String pictureString, int width, int height)
289 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
290 BitmapFactory.Options opt = new BitmapFactory.Options();
292 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
293 opt.inJustDecodeBounds = true;
295 BitmapFactory.decodeFile(pictureString, opt);
297 // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
307 // 画像の縮小サイズを決定する (縦幅、横幅の小さいほうにあわせる)
308 int widthBounds = opt.outWidth / width;
309 int heightBounds = opt.outHeight / height;
310 opt.inSampleSize=Math.min(widthBounds, heightBounds);
311 opt.inJustDecodeBounds = false;
314 return (BitmapFactory.decodeFile(pictureString, opt));