我已经阅读了标题中提到的有关此问题的一些内容,但我无法在我的代码中修复它。 只有调试器向我显示“无法在回收的位图上调用设置像素”并关闭应用程序。
所以很明显,要获得一个可变的位图并正确管理位图。但是我的错误在哪里?
我希望你能帮助我。
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localization);
this.bm = BitmapFactory.decodeResource(getResources(), R.drawable.raumstrukturapp);
this.picture = (ImageView) findViewById(R.id.picture);
this.picture.setImageResource(R.drawable.raumstrukturapp); calculatePositioniInPixel(infoccol.getBeaconRoomCollector().getBeaconRoom(0).getBeaconRoomeElements().get(0));
drawPosition(R.drawable.raumstrukturapp);
}
private void calculatePositioniInPixel(BeaconRoomElement bre) {
setKSUnit(9, 14);
this.coordInPixel[0][0] = bre.getElementcoordinates()[0][0] * this.xUnit;
Log.i("Beacons", "x links oben: " + this.coordInPixel[0][0]);
this.coordInPixel[0][1] = bre.getElementcoordinates()[0][1] * this.yUnit;
Log.i("Beacons", "y links oben: " + this.coordInPixel[0][1]);
this.coordInPixel[1][0] = bre.getElementcoordinates()[1][0] * this.xUnit;
Log.i("Beacons", "x rechts oben: " + this.coordInPixel[1][0]);
this.coordInPixel[1][1] = bre.getElementcoordinates()[1][1] * this.yUnit;
Log.i("Beacons", "y rechts oben: " + this.coordInPixel[1][1]);
this.coordInPixel[2][0] = bre.getElementcoordinates()[2][0] * this.xUnit;
Log.i("Beacons", "x rechts unten: " + this.coordInPixel[2][0]);
this.coordInPixel[2][1] = bre.getElementcoordinates()[2][1] * this.yUnit;
Log.i("Beacons", "y rechts unten: " + this.coordInPixel[2][1]);
this.coordInPixel[3][0] = bre.getElementcoordinates()[3][0] * this.xUnit;
Log.i("Beacons", "x links unten: " + this.coordInPixel[3][0]);
this.coordInPixel[3][1] = bre.getElementcoordinates()[3][1] + this.yUnit;
Log.i("Beacons", "y links unten: " + this.coordInPixel[3][1]);
}
...
private void drawPosition(int resID) {
bm = BitmapFactory.decodeResource(getResources(), R.drawable.raumstrukturapp);
mutableBm = convertToMutable(bm);
for(int i = (int) this.coordInPixel[0][0]; i <= this.coordInPixel[1][0]; i++) {
for(int j = (int)(this.coordInPixel[0][1]); j <= (this.coordInPixel[2][1]); j++) {
if(j > 0) {
mutableBm.setPixel(i, j, Color.rgb(255, 128, 0));
}
}
}
this.picture.setImageBitmap(mutableBm);
}
public static Bitmap convertToMutable(Bitmap imgIn) {
try {
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
int width = imgIn.getWidth();
int height = imgIn.getHeight();
Bitmap.Config type = imgIn.getConfig();
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes()*height);
imgIn.copyPixelsToBuffer(map);
imgIn.recycle();
System.gc();
imgIn = Bitmap.createBitmap(width, height, type);
map.position(0);
imgIn.copyPixelsFromBuffer(map);
channel.close();
randomAccessFile.close();
file.delete();
...
return imgIn;
}
...
}