Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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
Android 如何将我的GridView阵列图像保存到存储器?_Android - Fatal编程技术网

Android 如何将我的GridView阵列图像保存到存储器?

Android 如何将我的GridView阵列图像保存到存储器?,android,Android,我为我的图像制作了一个数组,以便在GridView列表中显示它们。问题是它占用了大量内存,滚动速度慢(不平滑),每次都会强制关闭。在检查解决方案后,我发现我需要将它们保存到存储中,然后在应用程序中使用它们。但它不起作用。有人能帮我怎么做吗 这是我的适配器代码: public class ImageAdapter extends BaseAdapter { Context mContext; // Keep all Images in array public Integer[] mThumbId

我为我的图像制作了一个数组,以便在GridView列表中显示它们。问题是它占用了大量内存,滚动速度慢(不平滑),每次都会强制关闭。在检查解决方案后,我发现我需要将它们保存到存储中,然后在应用程序中使用它们。但它不起作用。有人能帮我怎么做吗

这是我的适配器代码:

public class ImageAdapter extends BaseAdapter {
Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = { R.drawable.d, R.drawable.t,
        R.drawable.e, R.drawable.e, R.drawable.r,
        R.drawable.asdt, R.drawable.asd, R.drawable.a,
        //... etc

};

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize=4;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;
    imageView = new ImageView(mContext);

    imageView.setImageBitmap(decodeSampledBitmapFromResource(
            imageView.getResources(), mThumbIds[position], 100, 100));
    imageView.buildDrawingCache(isEmpty());
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new GridView.LayoutParams(
            (getScreenWidth() / 3), (getScreenWidth() / 3)));
    return imageView;

}

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public int getScreenWidth() {
    int columnWidth;
    WindowManager wm = (WindowManager) mContext
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    columnWidth = point.x;
    return columnWidth;
}
}

我只是使用它,让它为我做所有的工作

Picasso.with(context).load(R.drawable.whatever).into(imageView);
并完成。

在getView()中,而不是在setImageBitmap中。如果需要节省存储空间,可以使用库调整图像大小。