Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在gridview android中显示sd卡上的图像_Android_Gridview_Orientation - Fatal编程技术网

在gridview android中显示sd卡上的图像

在gridview android中显示sd卡上的图像,android,gridview,orientation,Android,Gridview,Orientation,我在GridView中显示sd卡上特定文件夹中的图像,直到知道所有工作正常,所有图像都显示在GridView上,但当我更改屏幕方向时,它崩溃了。下面是我的代码和崩溃日志: import java.io.File; import android.content.Context; import android.net.Uri; import android.os.Environment; import android.view.View; import android.view.ViewG

我在GridView中显示sd卡上特定文件夹中的图像,直到知道所有工作正常,所有图像都显示在GridView上,但当我更改屏幕方向时,它崩溃了。下面是我的代码和崩溃日志:

    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;
    }






}
我正在获取内存输出错误,下面是我的日志:

 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)


在manifest.xml中编辑活动标记,然后尝试将其添加到listview活动的清单中

android:configChanges="orientation|keyboardHidden"
如果这没有帮助,那么为图像文件添加这段代码以对位图进行采样

private Bitmap decodeFile(File f){
            FileOutputStream os = null;
            try {
                //Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;

                FileInputStream fis = new FileInputStream(f);
                BitmapFactory.decodeStream(fis, null, o);
                fis.close();

                int scale = 1;
                if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                    scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
                }

                //Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                fis = new FileInputStream(f);
                try {

                b = BitmapFactory.decodeStream(fis, null, o2);

                } catch (OutOfMemoryError e) {
                    Log.d("hai","filename"+f);
                    System.gc();
                }
                fis.close();
            } catch (IOException e) {
            }
            return b;
        }
加载大量图像会导致应用程序内存不足并强制关闭。我认为这就是您的应用程序发生的情况。内存问题是android在开发应用程序时遇到的一个复杂问题。这可以通过手动清除未使用的位图和使用垃圾收集器来解决

尝试使用System.gc()

尝试使用循环使用位图

Bitmap.recycle()

将所有未使用的位图设为空

释放所有未使用的内存


这一切都会对你有很大帮助,也会帮你解决这个问题。使用内存分析器,它会帮你找出释放的内存>试试这个

在提问之前你试过什么?在stackoverflow中搜索…在此之前,我使用BitmapFactory执行此操作,但在没有任何内存管理的情况下,没有任何东西可以很好地用于meLoad Full Image。您肯定会得到OOM错误,请参考此网站[位图)检查此…@SachinGurnani这是他遇到内存不足错误的链接。此解决方案不起作用。他必须解决内存不足的问题。我面临的问题与上面提到的相同。图像大小是否存在任何问题。如果图像大小较大,则可能出现oom异常。因此,缩小位图是一种简单的方法。检查我的answerFile[]files=root.listFiles();for(int i=0;i