发送图像块阵列时发出额外错误?

时间:2017-03-17 17:39:31

标签: java android image split

我想将图像分成16个部分并在GridView中显示。 但是在发送图像块阵列时出现错误。 错误是" E / JavaBinder:!!!失败的粘合剂交易!!!"

这个错误的解决方案是什么?

ImageParse.java

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class ImageParse extends AppCompatActivity {

    Button btn_split;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_parse);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        btn_split = (Button)findViewById(R.id.btn_split);
        image = (ImageView)findViewById(R.id.iv_split_image);

        btn_split.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                splitImage(image, 16);
            }
        });
    }

    private void splitImage(ImageView image, int chunkNumbers)
    {

        int rows, cols;

        int chunkHeight, chunkWidth;

        ArrayList<Bitmap> chunkedImages = new ArrayList<>(chunkNumbers);

        BitmapDrawable drawable = (BitmapDrawable)image.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
        rows = 4;
        cols = 4;
        chunkHeight = bitmap.getHeight() / rows;
        chunkWidth = bitmap.getWidth() / cols;

        int yCoord = 0;
        for(int x=0; x < rows; x++)
        {
            int xCoord = 0;
            for(int y=0; y < cols; y++)
            {
                chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }

        Intent intent = new Intent(ImageParse.this, ImageChunksActivity.class);
        Toast.makeText(this, "FLG1", Toast.LENGTH_SHORT).show();
        intent.putExtra("image_chunks", chunkedImages);

        startActivity(intent);



    }

}

ImageChunksActivity.java

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import android.widget.Toast;

import com.navigationmenu.ListViewAdapter.ImageAdapter;

import java.util.ArrayList;

public class ImageChunksActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_chunks);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ArrayList<Bitmap> imageChunks = getIntent().getParcelableExtra("image_chunks");

        GridView grid = (GridView) findViewById(R.id.gridImages);
        grid.setAdapter(new ImageAdapter(this, imageChunks));
        grid.setNumColumns((int) Math.sqrt(imageChunks.size()));

    }

}

ImageAdapter.java

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Bitmap> imageChunks;
    private int imageWidth;
    private int imageHeight;

    public ImageAdapter(Context c, ArrayList<Bitmap> images) {
        this.mContext = c;
        this.imageChunks = images;
        this.imageWidth = ((Bitmap)images.get(0)).getWidth();
        this.imageHeight = ((Bitmap)images.get(0)).getHeight();
    }

    public int getCount() {
        return this.imageChunks.size();
    }

    public Object getItem(int position) {
        return this.imageChunks.get(position);
    }

    public long getItemId(int position) {
        return (long)position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView image;
        if(convertView == null) {
            image = new ImageView(this.mContext);
            image.setLayoutParams(new AbsListView.LayoutParams(this.imageWidth - 10, this.imageHeight));
            image.setPadding(0, 0, 0, 0);
        } else {
            image = (ImageView)convertView;
        }

        image.setImageBitmap((Bitmap)this.imageChunks.get(position));
        return image;
    }
}

1 个答案:

答案 0 :(得分:0)

  

但是在发送图像块阵列时出现错误。错误是&#34; E / JavaBinder:!!!失败的粘合剂交易!!!&#34;

您的Intent太大了。它需要&lt; 1MB,实际上应该比这要小很多。

  

此错误的解决方案是什么?

可能性包括:

  • 将这些活动合并到一个活动中,使用片段或其他技术在该活动中切换UI,这样您就不需要尝试在活动之间传递大量数据

  • 在单例缓存管理器中缓存位图,请记住这是故意的内存泄漏,因此需要非常小心地实现