0

在 Java 中,我如何将图像(文件类型)编码为 base64。我正在尝试将此图像从 Android 应用程序上传到 Imgur。谢谢!

4

1 回答 1

0

好吧,如果您使用 Apache HTTP 客户端连接到服务。我建议使用 MultiPartEntity 提交图像。我不确定确切原因,但是当我尝试 Base64 时,当图像很大时遇到了问题(OOM 异常)。

使用 Apache HTTP 客户端提交图像。您需要做的就是创建一个 MultiPartEntity 并使用 addPart 添加图像。

public class UploadImagePayload extends MultiPartEntity {


public UploadImagePayload(ProgressListener listener, String imageFileUrl) {
    super(HttpMultipartMode.STRICT, listener);

    this.addPart("image", new FileBody(new File(imageFileUrl)));
    this.setStringPart("type", "file");
}

public void setAlbumId(String albumId) {
    this.setStringPart("album_id", albumId);
}

public void setName(String name) {
    this.setStringPart("name", name);
}

public void setTitle(String title) {
    this.setStringPart("title", title);
}

public void setDescription(String description) {
    this.setStringPart("description", description);
}

private void setStringPart(String name, String value) {
    try {
        addPart(name, new StringBody(value));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Log.e(TAG, "unable to set string property on image upload");
    }
}
}

然后只需创建上述类的实例并将数据设置为它:

    myEntity.addPart("image", new FileBody(new File(imageFileUrl)));
    myEntity.setStringPart("type", "file");

要发送请求,只需创建一个 HTTPPost,并将有效负载设置为您的实体:

    HttpPost post = new HttpPost(requestUrl);
    post.setEntity(myEntity);
    HttpClient client = new DefaultHttpClient();
    client.execute(post);

使用上面的方法相当简单,通过扩展 MultiPartEntiy 添加更多逻辑来添加进度监听真的很容易,例如:

public class BigMultiPartEntity extends MultipartEntity {

private ProgressListener listener;

public BigMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener)
{
    super(mode);
    this.listener = listener;
}

public BigMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)
{
    super(mode, boundary, charset);
    this.listener = listener;
}

@Override
public void writeTo(final OutputStream outstream) throws IOException
{
    super.writeTo(new CountingOutputStream(outstream, this.listener));
}

public interface ProgressListener {
    void transferred(int num);
}

public class CountingOutputStream extends FilterOutputStream
{
    private ProgressListener listener;
    private int transfered;

    /**
     * Creates an output stream filter built on top of the specified
     * underlying output stream.
     *
     * @param out the underlying output stream to be assigned to
     *            the field <tt>this.out</tt> for later use, or
     *            <code>null</code> if this instance is to be
     *            created without an underlying stream.
     */
    public CountingOutputStream(OutputStream out, ProgressListener listener) {
        super(out);
        this.listener = listener;
        this.transfered = 0;
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        this.transfered += len;
        //Log.d(TAG, "writing...transfer @ "+transfered);
        this.listener.transferred((int) ((transfered / (float) getContentLength()) * 100));
        out.write(b, off, len);


    }

    @Override
    public void write(int b) throws IOException {
        this.transfered += b;
        //Log.d(TAG, "writing...transfer @ "+transfered);
        this.listener.transferred((int) ((transfered / (float) getContentLength()) * 100));
        out.write(b);
    }
}
}

如果您没有使用 Apache HTTPClient,并且您肯定想使用 Base64,那么请查看: 如何将图像转换为 Base64 字符串?

如果您需要更多帮助,请查看位于 @ https://bitbucket.org/eggman87/ezimgur-open/src的我的 android imgur 应用程序的源代码。与 imgur 交谈的逻辑在 API 项目中。

于 2013-06-23T06:35:19.373 回答