android中拆分位图时的java.lang.IllegalArgumentException

android中拆分位图时的java.lang.IllegalArgumentException,android,bitmap,android-image,Android,Bitmap,Android Image,有人能告诉我为什么在分割位图时出现此错误吗。 代码: 公共静态列表ScambleImage(位图图像、int行、int列){ List scambledImage=new ArrayList(); int chunkWidth=image.getWidth();//cols int chunkHeight=image.getHeight();//行 int finalSize=chunkWidth/行; 位图bMapScaled=Bitmap.createScaledBitmap(图像、chun

有人能告诉我为什么在分割位图时出现此错误吗。 代码:

公共静态列表ScambleImage(位图图像、int行、int列){
List scambledImage=new ArrayList();
int chunkWidth=image.getWidth();//cols
int chunkHeight=image.getHeight();//行
int finalSize=chunkWidth/行;
位图bMapScaled=Bitmap.createScaledBitmap(图像、chunkWidth、chunkHeight、true);
int yCoord=0;//源中第一个像素的y坐标
对于(int x=0;x
行=6,列=6; 图像大小=648 x 484

这是个例外,但不知道如何进行修复:

java.lang.IllegalArgumentException: y + height must be <= bitmap.height()

java.lang.IllegalArgumentException:y+高度必须是您试图抓取原始位图中不存在的部分

在行上放置断点:

scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize,  finalSize));  
在第一次数组迭代之后的某个时候,您会看到它失败了,因为每次您的偏移量都是由xCoord/yCoord获取的bigmap的哪个“切片”的起点


我的直觉是,你对最终化的计算是错误的,但我只能猜测,因为我们不知道你到底想完成什么。

你试图获取原始位图中不存在的部分

在行上放置断点:

scambledImage.add(Bitmap.createBitmap(bMapScaled, xCoord, yCoord, finalSize,  finalSize));  
在第一次数组迭代之后的某个时候,您会看到它失败了,因为每次您的偏移量都是由xCoord/yCoord获取的bigmap的哪个“切片”的起点


我的直觉是,你对最终结果的计算是错误的,但我只能猜测,因为我们不知道你到底想完成什么。

我已经尝试了你的代码,并做了一些修改,它对我很有效

private ArrayList<Bitmap> splitImage(Bitmap bitmap, int rows, int cols){
    int chunks = rows*cols;
    int chunkHeight,chunkWidth;
    ArrayList<Bitmap> splittedImages = null;
    splittedImages = new ArrayList<Bitmap>(chunks);
    chunkHeight = bitmap.getHeight()/rows;
    chunkWidth = bitmap.getWidth()/cols;
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(), bitmap.getHeight(), true);
    int yCoord = 0;
    for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){
            splittedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }
    return splittedImages;
}
private ArrayList splitImage(位图、int行、int列){
int chunks=行*列;
int chunkHeight,chunkWidth;
ArrayList splittedImages=null;
splittedImages=新的ArrayList(块);
chunkHeight=bitmap.getHeight()/行;
chunkWidth=bitmap.getWidth()/cols;
位图缩放位图=位图.createScaledBitmap(位图,Bitmap.getWidth(),Bitmap.getHeight(),true);
int yCoord=0;

对于(intx=0;x,我已经尝试了您的代码,并做了一些更改,它对我很有用

private ArrayList<Bitmap> splitImage(Bitmap bitmap, int rows, int cols){
    int chunks = rows*cols;
    int chunkHeight,chunkWidth;
    ArrayList<Bitmap> splittedImages = null;
    splittedImages = new ArrayList<Bitmap>(chunks);
    chunkHeight = bitmap.getHeight()/rows;
    chunkWidth = bitmap.getWidth()/cols;
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap,bitmap.getWidth(), bitmap.getHeight(), true);
    int yCoord = 0;
    for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){
            splittedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }
    return splittedImages;
}
private ArrayList splitImage(位图、int行、int列){
int chunks=行*列;
int chunkHeight,chunkWidth;
ArrayList splittedImages=null;
splittedImages=新的ArrayList(块);
chunkHeight=bitmap.getHeight()/行;
chunkWidth=bitmap.getWidth()/cols;
位图缩放位图=位图.createScaledBitmap(位图,Bitmap.getWidth(),Bitmap.getHeight(),true);
int yCoord=0;

对于(int x=0;x感谢响应,我正在尝试将图像分割为6 x 6网格。感谢响应,我正在尝试将图像分割为6 x 6网格。