0

我可以从 url 创建输入流并将图像成功上传到 facebook。

HttpURLConnection conn = (HttpURLConnection) uploadFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
imgData = new byte[length];
InputStream is = conn.getInputStream();

但我正在从捆绑包中获取 Inputstream -

Bundle extras = getIntent().getExtras();
if (extras != null) {
    int img_pos = extras.getInt("image_pos");
    mInputStream = com.LiveWallapper.LiveWallapperActivity.mInput.get(img_pos);
    System.out.println("input in readbytes++++++++++++++" + mInputStream);
}

并想在 Example.java 中上传图片,例如-

params.putString("method", "photos.upload");
params.putByteArray("picture", readBytes(mInputStream));
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

readBytes 方法是——

public byte[] readBytes(InputStream inputStream) throws IOException {
    // this dynamically extends to take the bytes you read
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    // this is storage overwritten on each iteration with bytes

    // we need to know how may bytes were read to write them to the
    // byteBuffer

    int bufferSize = 10000;//i can not getcontent length from inputstream so i give //10000, pls help
    byte[] imgData = new byte[bufferSize];
    int len = 0;

    while ((len = inputStream.read(imgData)) != -1) {
        byteBuffer.write(imgData, 0, len);
    }

    // and then we can return your byte array.
    return byteBuffer.toByteArray();
}
4

0 回答 0