android附件上传下载(图片,语音,视频)

上传

所有的上传都是一样的都是转成流的形式发送给服务器,注意写入body是一个异步操作,我写的网络发送(okhttp+rxjava)也是异步的,所以在部分机型上会报错,java.net.ProtocolException: expected 6137 bytes but received 7081 ,这是因为还没有把所有数据写入body就发送了,所有可以在写入完成之后再发送。

 final File file = new File(path);//path为文件路径
        Observable.just(file).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .map(new Function<File, RequestBody>() {
                    @Override
                    public RequestBody apply(File file) throws Exception {
                        RequestBody requestFile =
                                RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        return requestFile;
                    }
                }).subscribe(new Observer<RequestBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(RequestBody requestBody) {
                final MultipartBody.Part part =
                        MultipartBody.Part.createFormData("uploadFile", file.getName(), requestBody);
                upload(part);//上传方法
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
//上传方法
 private void upload(MultipartBody.Part part) {
        Observable<HttpResult<FileUploadRec>> call = NetConnect.getService(HomeApi.class).uploadFile(part);
        call.compose(NetConnect.<HttpResult<FileUploadRec>>changeThread()).subscribe(new CallBackObserver<HttpResult<FileUploadRec>>(false) {
            @Override
            public void onSuccess(HttpResult<FileUploadRec> response) {
            }

            @Override
            public void onFailure(HttpResult<FileUploadRec> response){
            }
        });
    }
	/**文件上传*/
    @Multipart
    @POST("/protal-web/app/ftpUpload/ftpUpload.htm")
    Observable<HttpResult<FileUploadRec>> uploadFile(@Part MultipartBody.Part uploadFile);

下载

 /**下载文件*/
    @Streaming
    @GET
    Call<ResponseBody> downloadFile(@Url String filePath);
 Call<ResponseBody> call = NetConnect.getService(HomeApi.class).downloadFile(path);
                    call.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                        }
                    });

图片

从相册选择

/**
     * 相册选择
     */
    private void albumSelect() {
        //执行拍照前,应该先判断SD卡是否存在
        String SDState = Environment.getExternalStorageState();
        if (SDState.equals(Environment.MEDIA_MOUNTED)) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            MyApplication.getActivity().startActivityForResult(i, BaseParam.takePhotoResult);
        } else {
            Toast.makeText(MyApplication.getActivity(), "内存卡不存在", Toast.LENGTH_LONG).show();
        }
    }

回调为Uri data.getData();

 //通过uri得到路径
    public static String getRealPathFromURI(Context context, Uri contentURI) {
        String result;
        Cursor cursor = context.getContentResolver().query(contentURI,
                new String[]{MediaStore.Images.ImageColumns.DATA},//
                null, null, null);
        if (cursor == null) result = contentURI.getPath();
        else {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(index);
            cursor.close();
        }
        return result;
    }

相机拍摄

 /**
     * 相机拍摄
     */
    public void takeCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(MyApplication.getActivity().getPackageManager()) != null) {
            //创建文件
            File photoFile = createImageFile();
            if (photoFile != null) {
                //存入照片
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            }
        }
        MyApplication.getActivity().startActivityForResult(takePictureIntent, BaseParam.takeCameraResult);
    }
//创建图片文件
    private File createImageFile() {
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File image = null;
        try {
            image = File.createTempFile(
                    generateFileName(),  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
        mCurrentPath = image.getAbsolutePath();
        return image;
    }

    //生成文件名
    public static String generateFileName() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        return imageFileName;
    }

图片预览
图片预览采用的是glide框架

语音

语音采用的是按住发言方式录音

public class AudioRecoderUtils {
    //文件路径
    private String filePath;
    //文件夹路径
    private String FolderPath;

    private MediaRecorder mMediaRecorder;
    private final String TAG = "fan";
    public static final int MAX_LENGTH = 1000 * 60 ;// 最大录音时长1000*60;

    private OnAudioStatusUpdateListener audioStatusUpdateListener;

    /**
     * 文件存储默认sdcard/record
     */
    public AudioRecoderUtils(){

        //默认保存路径为/sdcard/record/下
        this(Environment.getExternalStorageDirectory()+"/record/");
    }

    public AudioRecoderUtils(String filePath) {

        File path = new File(filePath);
        if(!path.exists())
            path.mkdirs();

        this.FolderPath = filePath;
    }

    private long startTime;
    private long endTime;



    /**
     * 开始录音 使用amr格式
     *      录音文件
     * @return
     */
    public void startRecord() {
        // 开始录音
        /* ①Initial:实例化MediaRecorder对象 */
        if (mMediaRecorder == null)
            mMediaRecorder = new MediaRecorder();
        try {
            /* ②setAudioSource/setVedioSource */
//            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
//            /* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default 声音的(波形)的采样 */
//            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//            /*
//             * ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
//             * ,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
//             */
//            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            filePath = FolderPath + TimeUtils.getCurrentTime() + ".mp3" ;
            /* ③准备 */
            mMediaRecorder.setOutputFile(filePath);
            mMediaRecorder.setMaxDuration(MAX_LENGTH);
            mMediaRecorder.prepare();
            /* ④开始 */
            mMediaRecorder.start();
            // AudioRecord audioRecord.
            /* 获取开始时间* */
            startTime = System.currentTimeMillis();
            updateMicStatus();
            Log.e("fan", "startTime" + startTime);
        } catch (IllegalStateException e) {
            Log.i(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
        } catch (IOException e) {
            Log.i(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
        }
    }

    /**
     * 停止录音
     */
    public long stopRecord() {
        if (mMediaRecorder == null)
            return 0L;
        endTime = System.currentTimeMillis();

        try {

            mMediaRecorder.stop();
            mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaRecorder = null;

            audioStatusUpdateListener.onStop(filePath);
            filePath = "";

        }catch (RuntimeException e){
            mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaRecorder = null;

            File file = new File(filePath);
            if (file.exists())
                file.delete();

            filePath = "";

        }

        return endTime - startTime;
    }

    /**
     * 取消录音
     */
    public void cancelRecord(){
        try {

            mMediaRecorder.stop();
            mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaRecorder = null;

        }catch (RuntimeException e){
            mMediaRecorder.reset();
            mMediaRecorder.release();
            mMediaRecorder = null;
        }

        File file = new File(filePath);
        if (file.exists())
            file.delete();

        filePath = "";

    }

    private final Handler mHandler = new Handler();
    private Runnable mUpdateMicStatusTimer = new Runnable() {
        public void run() {
            updateMicStatus();
        }
    };


    private int BASE = 1;
    private int SPACE = 100;// 间隔取样时间

    public void setOnAudioStatusUpdateListener(OnAudioStatusUpdateListener audioStatusUpdateListener) {
        this.audioStatusUpdateListener = audioStatusUpdateListener;
    }

    /**
     * 更新麦克状态
     */
    private void updateMicStatus() {

        if (mMediaRecorder != null) {
            double ratio = (double)mMediaRecorder.getMaxAmplitude() / BASE;
            double db = 0;// 分贝
            if (ratio > 1) {
                db = 20 * Math.log10(ratio);
                if(null != audioStatusUpdateListener) {
                    audioStatusUpdateListener.onUpdate(db,System.currentTimeMillis()-startTime);
                }
            }
            mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);
        }
    }

    public interface OnAudioStatusUpdateListener {
        /**
         * 录音中...
         * @param db 当前声音分贝
         * @param time 录音时长
         */
        public void onUpdate(double db, long time);

        /**
         * 停止录音
         * @param filePath 保存路径
         */
        public void onStop(String filePath);
    }
}
//使用方式
 mAudioRecoderUtils = new AudioRecoderUtils();
        //录音回调
        mAudioRecoderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {

            //录音中....db为声音分贝,time为录音时长
            @Override
            public void onUpdate(double db, long time) {
            }

            //录音结束,filePath为保存路径
            @Override
            public void onStop(String filePath) {
            }
        });

语音播放预览

  /**
     * 播放音频
     */
    private void voicePlayer(String path) {
        File file = new File(path);
        System.out.println(path);
        Uri contentUri = FileProvider.getUriForFile(MyApplication.getActivity(), "包名.fileprovider", file);//android 7.0以后无法识别file://,需要转为uri
        MediaPlayer mediaPlayer = MediaPlayer.create(MyApplication.getActivity(), contentUri);
        mediaPlayer.start();
    }

视频

本地视频

  /**
     * 本地选择视频
     *
     * @return
     */
    public void localVedio() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        MyApplication.getActivity().startActivityForResult(intent, BaseParam.REQUEST_VIDEO_CODE);
    }
//回调获取路径
  Cursor cursor = MyApplication.getActivity().getContentResolver().query(data.getData(), null, null, null, null);
                cursor.moveToFirst();
                path = cursor.getString(1));

视频录制

 /***
     * 视频录制
     * @return
     */
    public void vedioButton() {
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        // 将文件存到指定的路径
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String filePath = storageDir + timeStamp + ".mp4";
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent();
        intent.setAction("android.media.action.VIDEO_CAPTURE");
        intent.addCategory("android.intent.category.DEFAULT");
        // 自定义输出位置,这样可以将视频存在我们指定的位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        //设置视频录制的最长时间
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
        //设置视频录制的画质
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.2);
        MyApplication.getActivity().startActivityForResult(intent, BaseParam.vedioResult);
    }

回调为Uri data.getData();
视频播放

/***
     * 调用系统自带的播放器
     */
    public void vedioPlayer(String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
         File file = new File(path);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "video/*");
        MyApplication.getActivity().startActivity(intent);
    }

附件上传可以适当的做一些压缩,常言的压缩就是图片压缩,语音本身不会太大,视频压缩比较麻烦,在拍摄的时候可以降低画质。服务器如果是nginx服务器可能会报413错误,因为其对文件做了限制,没改默认1M

发布了35 篇原创文章 · 获赞 16 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览