我开发了下面的类来接收带有标题(22字节)发送的图像,首先我解码标题以检查图像正确性然后我解码图像但并非所有图像都被解码,有时它返回SkImageDecoder::Factory returned null
。我正在使用这个Android 教程来inSampleImage
.
public class SocketServerStreamDataThread extends Thread {
private Socket streamSocket;
MainActivity mainActivity ;
ImageView displayedImage ;
Bitmap bitmapImageToDisplay = null;
byte[] dataBuffer = new byte[1048576];
boolean result ;
protected static boolean mReceivingStop; // Flag used to start and stop transmitting data
SocketServerStreamDataThread(Socket socket, MainActivity mainActivityReceived, ImageView displayedImageView) {
streamSocket = socket;
// received activity to work on it in our java file
mainActivity = mainActivityReceived ;
// received image UI component to display
displayedImage = displayedImageView;
}
@Override
public void run() {
/* Receiving the image */
try {
DecodeData decodeData = new DecodeData();
// call DecodeData to get Image data from stream
while (mReceivingStop == false) {
if (bitmapImageToDisplay == null) {
InputStream inputStream = streamSocket.getInputStream();
if (inputStream.read(dataBuffer) > -1) {
// Call the class to decode received stream and return integer with the state of the decoding of the received data
// result = 0 ; means that decoding header is successful.
// result = -1 ; means that there is a problem in decoding header.
byte [] dataHeaderReceived = Arrays.copyOf(dataBuffer, 23) ;
result = decodeData.iDecodeReceiveData(dataHeaderReceived);
// Evalute the received data
if (result == false) {
/* Fault on application */
/* Close socket */
streamSocket.close();
mReceivingStop = true;
}
if (result == true) {
/* Data have been received */
/* Show image */
BitmapFactory.Options imageOption = new BitmapFactory.Options();
imageOption.inJustDecodeBounds = true;
BitmapFactory.decodeResource(mainActivity.getResources(), R.id.display_image, imageOption);
imageOption.inSampleSize = calculateInSampleSize (imageOption, 550, 435) ;
imageOption.inJustDecodeBounds = false;
bitmapImageToDisplay = BitmapFactory.decodeByteArray(dataBuffer, 23, decodeData.imageSize, imageOption);
if (bitmapImageToDisplay != null) {
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
//try to display to image
displayedImage.setImageBitmap(bitmapImageToDisplay);
}
});
}
}
}
SystemClock.sleep(300);
bitmapImageToDisplay = null ;
}
}
streamSocket.close();
mReceivingStop = true;
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mainActivity, "Finished", Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
StringBuilder eMsg = new StringBuilder();
eMsg.append("Something wrong: " + e.getMessage());
final String eMessage=eMsg.toString();
// final String eMsg = "Something wrong: " + e.getMessage();
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mainActivity, eMessage, Toast.LENGTH_LONG).show();
}
});
} finally {
if(streamSocket != null){
try {
streamSocket.close();
mReceivingStop = true ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
日志输出:
GC_FOR_ALLOC freed 1307K, 27% free 13676K/18596K, paused 34ms, total 34ms
D/dalvikvm: GC_FOR_ALLOC freed 1306K, 27% free 13676K/18596K, paused 8ms, total 8ms
D/skia: --- SkImageDecoder::Factory returned null
D/skia: --- SkImageDecoder::Factory returned null
D/dalvikvm: GC_FOR_ALLOC freed 1607K, 29% free 13376K/18596K, paused 24ms, total 24ms
D/skia: --- SkImageDecoder::Factory returned null
D/dalvikvm: GC_FOR_ALLOC freed 1255K, 27% free 13728K/18596K, paused 9ms, total 9ms