イメージをソケットに接続する必要があります。現在、私は.jpgファイルといくつかの他の不要なバイト(ffd8)を含むバイト配列を持っています。これらの不要なバイトをバイト配列から削除する必要があります。バイトを削除せずにファイルをダウンロードしようとしましたが、イメージが破損した形式になっています。だから私は私のbytearrayから不要なバイトを削除する方法を知りたいです。ソケット接続でイメージをダウンロードして不要なバイトを取り除く方法
while (!status) {
byte[] buffer = new byte[1024]; // or 4096, or more
int bytesRead = input.read(buffer, 0, buffer.length);
int availableBytes = input.available();
str2 = str2.append(bytesRead);
ImageDataArray = concat(ImageDataArray, buffer);
countPackets = countPackets + buffer.length;
if (input.available() != 0) {
System.out.println("String:" + str1.toString());
} else break;
}
System.out.println("--------------------out of while ------------------");
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "/SteelmanPro/1020/images/image" +filename1);
if (!file.exists()) {
file.createNewFile();
}
}
write(ImageDataArray, Environment.getExternalStorageDirectory().getAbsoluteFile() + "/SteelmanPro/1020/images/image" +filename1);
CONCAT()メソッド:
public byte[] concat(byte[]...arrays)
{
// Determine the length of the result array
int totalLength = 0;
for (int i = 0; i < arrays.length; i++)
{
totalLength += arrays[i].length;
}
// create the result array
byte[] result = new byte[totalLength];
// copy the source arrays into the result array
int currentIndex = 0;
for (int i = 0; i < arrays.length; i++)
{
System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
currentIndex += arrays[i].length;
}
return result;
}
書き込み方法:ここに私のコードで
もいくつかの他の不要な.jpgファイルが含まれているvoid write(byte[] aInput, String aOutputFileName) {
Log.e("dfd", "Writing binary file...");
try {
OutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(aOutputFileName));
output.write(aInput);
} finally {
if (output != null)
output.close();
}
} catch (FileNotFoundException ex) {
Log.e("dfd", "File not found.");
} catch (IOException ex) {
Log.e("dfd", "" + ex);
}
}
十数個以上の空白行を削除しました。特にWeb上で垂直スペースを無駄にしないでください。 – EJP