Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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
毕加索内存不足错误-使用自定义适配器在ListView中加载Android图像_Android_Image_Out Of Memory_Adapter_Picasso - Fatal编程技术网

毕加索内存不足错误-使用自定义适配器在ListView中加载Android图像

毕加索内存不足错误-使用自定义适配器在ListView中加载Android图像,android,image,out-of-memory,adapter,picasso,Android,Image,Out Of Memory,Adapter,Picasso,所以现在我加入了毕加索和桑斯,每当我想做任何事情的时候,我都会经常出现记忆中的错误。这可能是因为毕加索缓存了这些图像吗?我完全不知道为什么会发生这种情况以及如何解决。。。有这样的经历吗 编辑:解决方案非常简单。必须按照毕加索的建议更改我的自定义适配器,以接收图像url并将其直接加载到图像视图,而不是使用另存为位图的迂回方式。忽略了提示,不知道为什么。 下面是适配器: public class ImageTextListViewAdapter extends ArrayAdapter<Row

所以现在我加入了毕加索和桑斯,每当我想做任何事情的时候,我都会经常出现记忆中的错误。这可能是因为毕加索缓存了这些图像吗?我完全不知道为什么会发生这种情况以及如何解决。。。有这样的经历吗

编辑:解决方案非常简单。必须按照毕加索的建议更改我的自定义
适配器
,以接收图像url并将其直接加载到
图像视图
,而不是使用另存为
位图的迂回方式。忽略了提示,不知道为什么。
下面是适配器:

public class ImageTextListViewAdapter extends ArrayAdapter<RowItem> {

Context context;

public ImageTextListViewAdapter(Context context, int resourceId,
                             List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txt;
    TextView id;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_friends, null);
        holder = new ViewHolder();

        holder.id = (TextView) convertView.findViewById(R.id.text_view_id_friends);
        holder.txt = (TextView) convertView.findViewById(R.id.list_item_friends_textview);
        holder.imageView = (ImageView) convertView.findViewById(R.id.friends_image);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txt.setText(rowItem.getText());
    String url = getItem(position).getUrl();

    Picasso.with(context).load(url).into(holder.imageView);
    holder.id.setText(rowItem.getId());

    return convertView;
}

您试图通过不使用现有的图像加载库来重新发明轮子。请查看一些顶级库:

如果你仍然决心自己建造它。您需要创建一个缓存系统,以便根据需要从内存/磁盘中保存/加载。还要小心泄漏视图,因为这可能会在图像加载(尤其是列表或网格)时变得非常棘手


android官方文档中有一个关于android的很好的教程。浏览一下,看看他们提供的帮助您入门的示例应用程序。

您试图通过不使用现有图像加载库来重新发明轮子。请查看一些顶级库:

如果你仍然决心自己建造它。您需要创建一个缓存系统,以便根据需要从内存/磁盘中保存/加载。还要小心泄漏视图,因为这可能会在图像加载(尤其是列表或网格)时变得非常棘手


android官方文档中有一个关于android的很好的教程。浏览一下,看看他们提供的帮助您入门的示例应用程序。

您不使用毕加索的原因是什么?可以省略该库,但您只需自行替换它的大部分功能即可。通用图像加载程序具有内置的切换选项。这是一个很好的库。我想我必须改变我的代码才能使用毕加索。谢谢你的帮助,我现在就用它:)你不使用毕加索的原因是什么?可以省略该库,但您只需自行替换它的大部分功能即可。通用图像加载程序具有内置的切换选项。这是一个很好的库。我想我必须改变我的代码才能使用毕加索。谢谢你的帮助,我现在就用它:)
 public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
    private final String LOG_TAG = ImageLoadTask.class.getSimpleName();
    String url, userId;
    String[] items;
    BufferedReader reader = null;

    public ImageLoadTask(String url, String userId, String[] items) {
        this.url = url;
        this.userId = userId;
        this.items = items;
    }

    private String getImageUrlFromJson(String imageJson) throws JSONException {
        JSONObject imageJsonOutput = new JSONObject(imageJson);
        imageJsonUrl = imageJsonOutput.getString("imageUrl");
        Log.v(LOG_TAG, imageJsonUrl);
        return imageJsonUrl;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        String imageJson = null;
        try {
            URL urlConnection = new URL(url + userId);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (input == null) {
                // Nothing to do.
                //jsonStr = null;
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(input));

            String line;
            while ((line = reader.readLine()) != null) {
                // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                // But it does make debugging a *lot* easier if you print out the completed
                // buffer for debugging.
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                return null;
            }
            imageJson = buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String imageUrl = getImageUrlFromJson(imageJson);
            URL url = new URL(imageUrl);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

            addBitmapToMemoryCache(userId, bmp);
            return bmp;
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        bmp = result;
        item = new RowItem(bmp, item[0], item[1], item[2]);
        mDiscoverAdapter.add(item);
    }
}