Android Q:文件上传(之前的文章有点问题)

前沿:之前写过两篇文章Android:IOException read fail:EBADF (Bad file descriptor)   和 Android Q:上传图片java.io.FileNotFoundException: open failed: EACCES (Permission denied) 用是可以用,但是隐含了个问题。而现在大部分博客提供的方法是把存储中的文件复制一份到内存中,再用传统的File file = new File(path)操作上传,也是可以的,但是中间多出了两步:复制和删除。

上面所写的博客,文件上传思路应当是没错的,获取文件Uri然后采用

ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(originalUri, "r");

FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

获取文件描述实体。

然后获取byte[],通过构建

RequestBody.create(MediaType.parse("multipart/form-data"), bytes); 生成Retrofit上传文件参数

其中问题就在于byte[],它是文件的所有字节编码。现在拍摄和录像,像素都非常大,直接将这些字节码加载到内存上传,就会造成oom或者崩溃(我这边是报错了)

显而易见的,不能把文件的字节码直接放到内存上,肯定是需要文件流缓冲池的。而OKHTTP早就有一套自己的IO操作封装 okio,其中包含BufferedSink(缓存池),Okio(操作封装),Source类(io读写)等,大致看了下,有误的望指出。

其实没什么难度的,从看源码能找到它的几个RequestBody构建方法,再从构建方法中找出,AndroidQ的文件系统能提供出来的参数。在构建RequestBody方法,有个之前使用的传入文件的方法:

/** Returns a new request body that transmits the content of {@code file}. */
  public static RequestBody create(final MediaType contentType, final File file) {
    if (file == null) throw new NullPointerException("content == null");

    return new RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() {
        return file.length();
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
          source = Okio.source(file);
          sink.writeAll(source);
        } finally {
          Util.closeQuietly(source);
        }
      }
    };
  }

通过我们要抛弃File不用,只需要关注Okio.source(file),看它其中是怎么实现的:

/** Returns a source that reads from {@code file}. */
  public static Source source(File file) throws FileNotFoundException {
    if (file == null) throw new IllegalArgumentException("file == null");
    return source(new FileInputStream(file));
  }

当到这一步的时候,那就是非常简单了,上文中可以通过fileDescriptor获取到FileInputStream,只需要把这个file替换成FileInputStream就行了,这是我自己改的一个RequestBody,基本一样,只是最后加了一个Create:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.internal.Util;
import okio.BufferedSink;
import okio.ByteString;
import okio.Okio;
import okio.Source;

public abstract class RequestBody {
  /** Returns the Content-Type header for this body. */
  public abstract MediaType contentType();

  /**
   * Returns the number of bytes that will be written to {@code out} in a call to {@link #writeTo},
   * or -1 if that count is unknown.
   */
  public long contentLength() throws IOException {
    return -1;
  }

  /** Writes the content of this request to {@code out}. */
  public abstract void writeTo(BufferedSink sink) throws IOException;

  /**
   * Returns a new request body that transmits {@code content}. If {@code contentType} is non-null
   * and lacks a charset, this will use UTF-8.
   */
  public static okhttp3.RequestBody create(MediaType contentType, String content) {
    Charset charset = Util.UTF_8;
    if (contentType != null) {
      charset = contentType.charset();
      if (charset == null) {
        charset = Util.UTF_8;
        contentType = MediaType.parse(contentType + "; charset=utf-8");
      }
    }
    byte[] bytes = content.getBytes(charset);
    return create(contentType, bytes);
  }

  /** Returns a new request body that transmits {@code content}. */
  public static okhttp3.RequestBody create(final MediaType contentType, final ByteString content) {
    return new okhttp3.RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() throws IOException {
        return content.size();
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        sink.write(content);
      }
    };
  }

  /** Returns a new request body that transmits {@code content}. */
  public static okhttp3.RequestBody create(final MediaType contentType, final byte[] content) {
    return create(contentType, content, 0, content.length);
  }

  /** Returns a new request body that transmits {@code content}. */
  public static okhttp3.RequestBody create(final MediaType contentType, final byte[] content,
                                   final int offset, final int byteCount) {
    if (content == null) throw new NullPointerException("content == null");
    Util.checkOffsetAndCount(content.length, offset, byteCount);
    return new okhttp3.RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() {
        return byteCount;
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        sink.write(content, offset, byteCount);
      }
    };
  }

  /** Returns a new request body that transmits the content of {@code file}. */
  public static okhttp3.RequestBody create(final MediaType contentType, final File file) {
    if (file == null) throw new NullPointerException("content == null");

    return new okhttp3.RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() {
        return file.length();
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
          source = Okio.source(file);
          sink.writeAll(source);
        } finally {
          Util.closeQuietly(source);
        }
      }
    };
  }

  public static okhttp3.RequestBody create(final MediaType contentType, final InputStream is) {
    if (is == null) throw new NullPointerException("content == null");

    return new okhttp3.RequestBody() {
      @Override public MediaType contentType() {
        return contentType;
      }

      @Override public long contentLength() throws IOException {
        return is.available();
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
          source = Okio.source(is);
          sink.writeAll(source);
        } finally {
          Util.closeQuietly(source);
        }
      }
    };
  }

}

只需要看到最后一个就行了,这时候构建RequestBody就是:

public static List<MultipartBody.Part> getFileBody(FileInputStream fis, String fileName) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        RequestBody body = com.zh.jiang.yin.utils.RequestBody.create(MediaType.parse("multipart/form-data"), fis);//表单类型

        builder.addFormDataPart("file", fileName, body);
        return builder.build().parts();
    }


//附上api接口
    /**
     * 文件上传
     */
    @Multipart
    @POST("file-application/upload")
    Observable<ObjResponse<ShowImageTypeData>> updateFile(@Part List<MultipartBody.Part> partLis);

例子就不贴上来了,实际体验成功,需要和 Android Q:上传图片java.io.FileNotFoundException: open failed: EACCES (Permission denied) 一起看,不一起整合是想自己做个记录,这个问题本身不算特别难

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值