Android 无法将新创建的位图流写入 WCF 服务

标签 android wcf inputstream

我在将位图流写入 WCF DataOutputStream 请求时遇到了一个奇怪的问题。我得到:

IOException: expected bytes XXX but received XXX

我已经使用 FileInputStream 对其进行了测试,它可以完美地上传,但是当我将位图转换为 InputStream 时,它会抛出 IOException。任何帮助将不胜感激。

下面是我调用 AsyncTask 的函数:

public static Boolean AddAdItemImage(int AdItemId,File ThisFile)
{
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    InputStream fileInputStream = null;
    ByteArrayOutputStream stream = null;

    try 
    {    
        if (ThisFile.isFile()) 
        {
            Bitmap Bm = Utility.decodeSampledBitmapFromResource(ThisFile.getPath(), 640, 480);
            stream = new ByteArrayOutputStream();
            Bm.compress(CompressFormat.JPEG, 100, stream);
            fileInputStream = new ByteArrayInputStream(stream.toByteArray());

            //fileInputStream = new FileInputStream(ThisFile);

            conn = (HttpURLConnection) new URL(Params.GetServiceUrl()+"/AddAdImage?AdItemId="+String.valueOf(AdItemId)+"&ContentType="+Utility.GetFileMimeType(ThisFile)+"&apikey="+Params.GetApiKey()).openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");

            conn.setFixedLengthStreamingMode((int) ThisFile.length());// works fine till 24 mb file
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type","application/stream");

            dos = new DataOutputStream(conn.getOutputStream());

            int maxBufferSize =  1 * 1024 * 1024;
            int bytesAvailable = fileInputStream.available();
            int bufferSize = Math.min(bytesAvailable,maxBufferSize);

            byte[] buffer = new byte[bufferSize];

            Log.i("Upload Ad Image", "Writing Stream");

            while ((bufferSize = fileInputStream.read(buffer)) > 0)
            {
                dos.write(buffer, 0, bufferSize);
            }

            Log.i("Upload Ad Image", "Stream Written");

            //dos.write(stream.toByteArray());

            String lineEnd = "";

            dos.writeBytes(lineEnd);

            Log.i("Upload Ad Image", "Line Written");

            // read the SERVER RESPONSE
            inStream = new DataInputStream(conn.getInputStream());
            String str = new String();
            String strResponse = new String();

            while ((str = inStream.readLine()) != null) 
            {
                strResponse += str;
            }

            inStream.close();

            dos.flush();
            dos.close();

            return Boolean.valueOf(strResponse);
        } 
    }
    catch (MalformedURLException ex)
    {
        Log.e("AddAdItemImage", "MalformedURLException: " + ex.getMessage(),ex);
    } 
    catch (IOException ioe)
    {
        Log.e("AddAdItemImage", "IOException: " + ioe.getMessage(),ioe);
    }
    catch (IllegalStateException e)
    {
        Log.e("AddAdItemImage", "IllegalStateException: " + e.getMessage(),e);
    }
    catch (Exception e)
    {
        Log.e("AddAdItemImage", "Exception: " + e.getMessage(),e);
    }
    finally
    {
        try
        {
            if(fileInputStream != null)
                fileInputStream.close();
        }
        catch (IOException e){} 

        try 
        {   
            if(stream!=null)
                stream.close();
        }
        catch (IOException e) {}
    }   

    return false;
}

最佳答案

我解决了这个问题,就是这一行:

conn.setFixedLengthStreamingMode((int) ThisFile.length()); 

文件内容长度与新创建的位图不同。我想知道我怎么能花将近两天的时间来找到解决方案;我想有时候你只需要跳出框框思考 :D

关于Android 无法将新创建的位图流写入 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13226876/

相关文章:

Web 服务和单例 WCF 服务的 WCF 生命周期?

java - byte[] 输入流转换为字符串

java - Toast 适用于 Java Activity ,但不适用于 kotlin

java - 如何设置带角的表面 View 相机背景

android - 如何解决此错误,该错误在加载内容之前使屏幕随机变黑?

java - 导入文本文件并在 Java 中逐行读取

Java - 当不需要写入文件时 - 我应该使用输入流还是输出流?

android - 不会共享的应用程序是否需要内容提供程序?

c# - WCF 服务异常 :The formatter threw an exception while trying to deserialize the message

c# - 使用 WCF 服务开发 iOS 应用程序