Ivam面臨這個內存問題,它讓我瘋狂。 我的應用程序正在接收來自樹莓的二進制數據。有了這個數據我創建一個int數組,而不是使用它來創建我的位圖和顯示圖像的顏色。傳輸協議是UDP。 當我收到一張圖像時沒有問題,但是當我要求我的樹莓發送10張圖像時,此消息出現GC_FOR_ALLOC,有時傳送停止而沒有發送所有圖像。 我googled這個問題,我添加了Runtime.getRuntime()。gc();的System.gc();我的代碼和同樣的問題。 我將所有對象都聲明爲循環外,以避免每次創建新變量,使用createBitmap(int []顏色,int寬度,int高度,Bitmap.Config配置)創建我的位圖Im時,我在互聯網上閱讀執行此方法並不好,而使用createScaledBitmap(位圖src,int dstWidth,int dstHeight,布爾過濾器)。 問題我不知道如何使用我的數組顏色與createScaledBitmap方法。 另外,我在我的循環結束時嘗試了bitmap.recycle,它發送第一個圖像,然後我有一個錯誤,「試圖使用回收」。 請有人可以幫助我。 我可以添加我的代碼,如果你想。 謝謝 這是代碼:很多GC_FOR_ALLOC,有時當這個警告是無限的時候停止傳輸
class UDPServer {
MainActivity act;
final String capitalizedSentence = "well received!";
final byte[] sendData = capitalizedSentence.getBytes();
public UDPServer(MainActivity act) {
this.act=act;
}
public void connect() throws Exception {
final ImageView image = (ImageView) act.findViewById(R.id.imageDisplay);
final byte[] receiveData1=new byte[40960], receiveData2=new byte[40960], receiveData3=new byte[40960], receiveData4=new byte[40960];
final DatagramSocket serverSocket=new DatagramSocket(8080);
final DatagramPacket receivePacket1 = new DatagramPacket(receiveData1, receiveData1.length);
final DatagramPacket receivePacket2 = new DatagramPacket(receiveData2, receiveData2.length);
final DatagramPacket receivePacket3 = new DatagramPacket(receiveData3, receiveData3.length);
final DatagramPacket receivePacket4 = new DatagramPacket(receiveData4, receiveData4.length);
final Thread udpserver = new Thread()
{
public void run() {
try {
while (!isInterrupted()) {
System.out.println("waiting for the conx");
serverSocket.receive(receivePacket1);
serverSocket.receive(receivePacket2);
serverSocket.receive(receivePacket3);
serverSocket.receive(receivePacket4);
System.out.println("thank you sir");
final Bitmap bitmap=Bitmap.createBitmap(act.afficheImage(
receivePacket1.getData(),
receivePacket2.getData(),
receivePacket3.getData(),
receivePacket4.getData()),
320, 256, Bitmap.Config.ARGB_8888);
act.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
image.setImageBitmap(bitmap);
}
});
InetAddress IPAddress = receivePacket1.getAddress();
int port = receivePacket1.getPort();
System.out.println("From: " + IPAddress + ":" + port);
DatagramPacket sendPacket1 = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket1);
Runtime.getRuntime().gc();
System.gc();
}
serverSocket.close();
} catch (SocketException ex) {
System.out.println("UDP Port 8080 is occupied.");
} catch (IOException ex1) {
System.out.println("Error: " + ex1.getMessage());
ex1.printStackTrace();
}
}
};
udpserver.start();
}
}
這是方法afficheImage:
private static final int WIDTH = 320;
private static final int HEIGHT = 256;
public static int a=255<<24;
public static ByteBuffer buffer1;
public static short[] array=new short[WIDTH * HEIGHT];
public static byte[] si=new byte[2];
public static int[] colors = new int[WIDTH * HEIGHT];
public static int[] afficheImage(byte[] s1, byte[] s2, byte[] s3, byte[] s4){
try{
for (int i=0;i<HEIGHT*WIDTH/4;i++){
si[0] = s1[2*i];
si[1] = s1[2*i+1];
buffer1 = ByteBuffer.wrap(si);
buffer1.order(ByteOrder.LITTLE_ENDIAN);
array[i]=buffer1.getShort();
}
for (int i=0;i<HEIGHT*WIDTH/4;i++){
si[0] = s2[2*i];
si[1] = s2[2*i+1];
buffer1 = ByteBuffer.wrap(si);
buffer1.order(ByteOrder.LITTLE_ENDIAN);
array[i+s1.length/2]=buffer1.getShort();
}
for (int i=0;i<HEIGHT*WIDTH/4;i++){
si[0] = s3[2*i];
si[1] = s3[2*i+1];
buffer1 = ByteBuffer.wrap(si);
buffer1.order(ByteOrder.LITTLE_ENDIAN);
array[i+s1.length]=buffer1.getShort();
}
for (int i=0;i<HEIGHT*WIDTH/4;i++){
si[0] = s4[2*i];
si[1] = s4[2*i+1];
buffer1 = ByteBuffer.wrap(si);
buffer1.order(ByteOrder.LITTLE_ENDIAN);
array[i+3*s1.length/2]=buffer1.getShort();
}
int delta=foundMax(array)-foundMin(array);
int min=foundMin(array);
int r;
for (int i=0;i<HEIGHT*WIDTH;i++) {
r = (array[i] - min) * 255/delta;
colors[i] = a | (r << 16) | (r << 8) | r;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return colors;
}
Raspberry發送的圖像的尺寸是多少?另外,你是否知道像素配置,例如ARGB_8888? –
我早些時候回答了關於解碼圖像的問題。 Raspberry是否會向您發送像PNG或JPEG這樣的編碼圖像,而不是原始像素數據? –
請用你正在使用的代碼更新你的問題。 –