Android:将图像(类型为File)编码到Base64

时间:2013-06-09 20:58:58

标签: java android image base64 imgur

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

1 个答案:

答案 0 :(得分: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,那么请查看: How to convert a image into Base64 string?

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