gpt4 book ai didi

android - 从 SD 卡加载图像时的 gridview 和 setImageBitmap

转载 作者:行者123 更新时间:2023-11-29 22:21:38 25 4
gpt4 key购买 nike

我想创建一个 GridView 来从 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/>

最佳答案

在 GridView 中,您不需要原始分辨率图像来显示 sdcard 中的图像。您始终可以使用 BitmapFactory.Options.inSampleSize 来获取较小尺寸的图像。这不会导致任何内存异常。

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 5; //any value greater than 1
Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + fileNames[position],opt);

关于android - 从 SD 卡加载图像时的 gridview 和 setImageBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7026591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com