Android的开发图片上传损坏(Android Development Image Upload

2019-09-26 15:55发布

我开发一个Android应用程序,我试图将图像上传到我们从客户端服务器。 当我到服务器,以查看它已损坏的图像(上底灰色条)有时。 我不知道为什么会这样。 谁能帮我解决这个问题或点我就如何做到这一点的指导? 下面是上传图像的代码:

try {
    url = new URL(SERVER_URL);
    connection = (HttpURLConnection)url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "image/jpeg");
    connection.setChunkedStreamingMode(STREAM_CHUNK_SIZE_KB * 1024);
    connection.connect();
    outputStream = new DataOutputStream(connection.getOutputStream());

    // Write file header (userId; Id; contentId; MediaType; size)
    outputStream.writeLong(mUserId);
    outputStream.writeLong(mId);
    outputStream.writeLong(file.getId());
    outputStream.writeUTF(MediaType.getMediaType(file.getMediaType()));
    outputStream.writeInt(file.getSize());

    // Write file data to stream
    int maxBufferSize = (8 * 1024);
    FileInputStream fileInputStream = new FileInputStream(file.getImageFile());
    int bytesAvailable = fileInputStream.available();
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];
    int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0) {
        outputStream.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes("\r\n");
    outputStream.flush();

    // Check response
    int responseCode = connection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        uploadStatus = true;
        InputStream responseStream = connection.getInputStream();
        BufferedReader responseReader = new BufferedReader(new 
            InputStreamReader(responseStream));
        char[] response = new char[4];
        responseReader.read(response, 0, 4);
        responseReader.close();
        int responseValue = 0;
        for (int b = 0; b < 4; b++) {
            responseValue |= response[b] & 0xFF;
            if (b < 3) {
                responseValue <<= 8;
            }
        }

        switch (responseValue) {
            case SAVED_SUCCESSFULLY:
                Log.d("FileUploader::upload -> Server response: Upload successful");
            break;
            case ERROR_SAVING_FILE:
                Log.d("FileUploader::upload -> Server response: Upload failed");
            break;
            case FILE_MORE_THAN_ALLOWED_SIZE:
                Log.d("FileUploader::upload -> Server response: Upload failed, exceeded 
                    allowed file size");
            break;
        }
    } 

    else {
        Log.d("FileUploader::upload -> responseCode = " + responseCode);
        checkErrorStream(connection.getErrorStream());
    }
} 

catch(Exception e) {
    Log.e(Log.getStackTraceString(e));
} 

finally {
    try {
        if (outputStream != null) {
            outputStream.close();
        }
    } 

    catch(Exception e) {
        Log.e(Log.getStackTraceString(e));
    }

    if (connection != null) {
        connection.disconnect();
    }
}

Answer 1:

请尝试通过USB不通过网络把图像从divece。 请问这是图像确定或将它太破了?



Answer 2:

我不能轻易告诉你为什么图像无需建立一个测试应用程序并运行你的代码的破坏,但我可以给你一个提示:

原始图像保存到一个文件中。 抓住这是转移在网络上的形象。 打开他们都在十六进制编辑器,然后看看有什么区别。 (这大概是接近文件的结尾。)

这应该告诉你实际的腐败组成,这反过来会告诉你去寻找你的代码什么。

(例如,你可能会截断您的文件,或追加一个换行符是不应该存在,或任意数量的细微的错误。)



Answer 3:

你可能在你的代码中的错误:

outputStream.write(buffer, 0, bufferSize);

应该

outputStream.write(buffer, 0, bytesRead);


文章来源: Android Development Image Upload Corrupted