短视频APP开发,调用系统相册,选择音频、视频、图片

短视频APP开发,调用系统相册,选择音频、视频、图片相关的代码

package com.example.mediacodectest.utils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import static android.app.Activity.RESULT_OK;

public class SelectMediaUtil {

    private final int REQUEST_CODE_SELECT = 112;

    private String path;
    private Activity activity;

    private SelectType selectType = SelectType.video;

    public enum SelectType {
        audio(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MediaStore.Audio.Media.DATA),
        video(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.DATA),
        image(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA);

        Uri externalContentUri;
        String data;

        SelectType(Uri externalContentUri, String data) {
            this.externalContentUri = externalContentUri;
            this.data = data;
        }
    }

    public void select(Activity activity, SelectType selectType) {
        this.activity = activity;
        this.selectType = selectType;
        Intent i = new Intent(Intent.ACTION_PICK, selectType.externalContentUri);
        activity.startActivityForResult(i, REQUEST_CODE_SELECT);
    }

    public String onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CODE_SELECT && resultCode == RESULT_OK && null != data) {
            Uri uri = data.getData();
            String[] filePathColumn = {selectType.data};

            Cursor cursor = activity.getContentResolver().query(uri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);
            cursor.close();
        }

        return path;
    }

}

以上就是 短视频APP开发,调用系统相册,选择音频、视频、图片相关的代码,更多内容欢迎关注之后的文章

上一篇:疫情被裁,闭关60天看完这份PDF,看我如何拿下腾讯、华为、字节跳动的offer


下一篇:阿里云联合思普瑞上线平安社区服务平台 提供物联网一站式管理