Я хочу создать сетку для загрузки изображения с SD-карты.Я нахожу много информации в Интернете, но не могу этого сделать.
Когда я запускаю приведенный ниже код, он принудительно закрывается.
Я обнаружил, что проблема imageView.setImageBitmap(bmp);
Но я не знаю, что не так с этим кодом.
Я надеюсь, что кто-то может помочь мне решить эту проблему.
public class GridActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_activity);
GridView gridview = (GridView)findViewById(R.id.GridView01);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(GridActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
File dir=new File(Environment.getExternalStorageDirectory(),"/images/bedroom");
int count=dir.list().length;
String[] fileNames = dir.list();
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
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;
}
Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + fileNames[position]);
imageView.setImageBitmap(bmp);
toast(dir.getPath() + "/" + fileNames[position]);
return imageView;
}
public View getView1(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}
}
public void toast (String msg)
{
Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
}
}
Журнал ошибок:
08-11 23:52:38.874: ERROR/MBGlossaryManager(5395): lib_glossary.so can't be loaded<br/>
08-11 23:52:49.710: ERROR/dalvikvm-heap(5395): 10036224-byte external allocation too large for this process.<br/>
08-11 23:52:49.710: ERROR/(5395): VM won't let us allocate 10036224 bytes.<br/>
08-11 23:52:49.710: ERROR/AndroidRuntime(5395): Uncaught handler: thread main exiting due to uncaught exception.<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): java.lang.OutOfMemoryError: bitmap size exceeds VM budget.<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.easyhome.GridActivity$ImageAdapter.getView(GridActivity.java:81).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.AbsListView.obtainView(AbsListView.java:1291).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.makeAndAddView(GridView.java:1218).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.makeRow(GridView.java:265).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.fillDown(GridView.java:218).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.fillFromTop(GridView.java:337).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.GridView.layoutChildren(GridView.java:1112).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.AbsListView.onLayout(AbsListView.java:1130).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.onLayout(LinearLayout.java:918).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.FrameLayout.onLayout(FrameLayout.java:333).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.LinearLayout.onLayout(LinearLayout.java:918).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942)
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.widget.FrameLayout.onLayout(FrameLayout.java:333).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.View.layout(View.java:6942).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.ViewRoot.performTraversals(ViewRoot.java:1007).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.view.ViewRoot.handleMessage(ViewRoot.java:1656).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.os.Handler.dispatchMessage(Handler.java:99).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.os.Looper.loop(Looper.java:136).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at android.app.ActivityThread.main(ActivityThread.java:4425).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at java.lang.reflect.Method.invokeNative(Native Method).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at java.lang.reflect.Method.invoke(Method.java:521).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608).<br/>
08-11 23:52:49.764: ERROR/AndroidRuntime(5395): at dalvik.system.NativeStart.main(Native Method).<br/>