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.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;
15 import jp.sourceforge.gokigen.memoma.Main;
16 import jp.sourceforge.gokigen.memoma.R;
24 public class ImageLoader
26 ProgressDialog loadingDialog = null;
27 Context parent = null;
30 String imageFile = null;
31 Bitmap imageBitmap = null;
34 ImageView imageView = null;
36 public ImageLoader(Context context)
38 loadingDialog = new ProgressDialog(context);
44 * (一度も動かしたことのないコードなので注意!)
46 * @return イメージファイル名の一覧
49 public static String[] getImageFileList(Activity activity)
53 HashSet<String> list = new HashSet<String>();
55 Cursor c = activity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
58 String imagefile = c.getString(c.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
59 File file = new File(imagefile);
60 list.add(file.getParent());
62 return (String[]) list.toArray(new String[list.size()]);
78 public static Uri parseUri(String imageFile)
80 if (imageFile.startsWith("content://") == true)
82 return (Uri.parse(imageFile));
85 File picFile = new File(imageFile);
86 return (Uri.fromFile(picFile));
93 public static void setImage(Context context, ImageView view, String imageFile)
97 int width = view.getWidth();
98 int height = view.getHeight();
99 if (imageFile.startsWith("content://") == true)
101 // URIから画像を設定する...OutOfMemory対策付き
102 bitmap = getBitmapFromUri(context, Uri.parse(imageFile), width, height);
106 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
107 bitmap = getBitmap(imageFile, view.getWidth(), view.getHeight());
109 view.setScaleType(ImageView.ScaleType.FIT_XY);
110 view.setImageBitmap(bitmap);
114 * 画面にイメージを表示する (ロード中ダイアログ表示つき)
117 public void setImage(ImageView view, String targetFile)
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();
127 imageFile = targetFile;
130 imageWidth = view.getWidth();
131 imageHeight = view.getHeight();
137 Thread thread = new Thread(new Runnable()
143 if (imageFile.startsWith("content://") == true)
145 // URIから画像を設定する...OutOfMemory対策付き
146 imageBitmap = getBitmapFromUri(parent, Uri.parse(imageFile), imageWidth, imageHeight);
150 // OutOfMemory対策付き...ビットマップのサイズを圧縮して表示
151 imageBitmap = getBitmap(imageFile, imageWidth, imageHeight);
153 handler.sendEmptyMessage(0);
157 handler.sendEmptyMessage(0);
164 private final Handler handler = new Handler()
167 public void handleMessage(Message msg)
169 if ((imageBitmap != null)&&(imageView != null))
171 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
172 imageView.setImageBitmap(imageBitmap);
180 loadingDialog.dismiss();
196 * URI経由でビットマップデータを取得する
204 public static Bitmap getBitmapFromUri(Context context, Uri uri, int width, int height)
206 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
207 BitmapFactory.Options opt = new BitmapFactory.Options();
209 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
210 opt.inJustDecodeBounds = true;
212 opt.inPurgeable = true;
213 opt.inPreferredConfig = Bitmap.Config.RGB_565;
215 InputStream input = null;
218 input = context.getContentResolver().openInputStream(uri);
219 BitmapFactory.decodeStream(input, null, opt);
224 Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString());
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());
284 * @param pictureString
289 public static Bitmap getBitmap(String pictureString, int width, int height)
292 // ファイルの表示方法を若干変更する ⇒ Uri.Parse() から BitmapFactoryを利用する方法へ。
293 BitmapFactory.Options opt = new BitmapFactory.Options();
295 // OutOfMemoryエラー対策...一度読み込んで画像サイズを取得
296 opt.inJustDecodeBounds = true;
298 BitmapFactory.decodeFile(pictureString, opt);
300 // 表示サイズに合わせて縮小...表示サイズが取得できなかった場合には、QVGAサイズと仮定する
310 // 画像の縮小サイズを決定する (縦幅、横幅の小さいほうにあわせる)
311 int widthBounds = opt.outWidth / width;
312 int heightBounds = opt.outHeight / height;
313 opt.inSampleSize=Math.min(widthBounds, heightBounds);
314 opt.inJustDecodeBounds = false;
317 return (BitmapFactory.decodeFile(pictureString, opt));