imグリッドビューのSDカードの特定のフォルダから画像を表示すると、すべてが正常に機能することがわかりますが、すべての画像がグリッドビューに表示されますが、画面の向きを変更するとクラッシュします。以下は私のコードとクラッシュログです:
import java.io.File;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MYPICS" + File.separator);
private File[] fileName = root.listFiles();
int count=fileName.length;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return fileName.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Uri uri = Uri.fromFile(fileName[position]);
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageURI(uri);
return imageView;
}
}
メモリ出力エラーが発生します。以下は私のLogcatです:
05-16 16:18:31.218: E/AndroidRuntime(26907): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.ImageView.resolveUri(ImageView.java:521)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.ImageView.setImageURI(ImageView.java:305)
05-16 16:18:31.218: E/AndroidRuntime(26907): at com.sachin.imagegrideview.ImageAdapter.getView(ImageAdapter.java:53)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.AbsListView.obtainView(AbsListView.java:1464)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.GridView.onMeasure(GridView.java:935)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.os.Looper.loop(Looper.java:130)
05-16 16:18:31.218: E/AndroidRuntime(26907): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-16 16:18:31.218: E/AndroidRuntime(26907): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 16:18:31.218: E/AndroidRuntime(26907): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 16:18:31.218: E/AndroidRuntime(26907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-16 16:18:31.218: E/AndroidRuntime(26907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-16 16:18:31.218: E/AndroidRuntime(26907): at dalvik.system.NativeStart.main(Native Method)