Android读取本地sd卡图片和视频文件

 博主介绍:本人专注于Android/java/数据库/微信小程序技术领域的开发,以及有好几年的计算机毕业设计方面的实战开发经验和技术积累;尤其是在安卓(Android)的app的开发和微信小程序的开发,很是熟悉和了解;本人也是多年的Android开发人员;希望我发布的此篇文件可以帮助到您;

效果演示

一、跳转页面

1:选择图片

Intent choiceImage = new Intent(this, ChoiceLocalFileActivity.class);
choiceImage.putExtra("msg", "img");
this.startActivityForResult(choiceImage, 10);

2:选择视频
Intent choiceVideo = new Intent(this, ChoiceLocalFileActivity.class);
choiceVideo.putExtra("msg", "video");
this.startActivityForResult(choiceVideo, 10);

二、获取文件列表

1:获取图片列表

List<FileVideolModel> getImage() {
    String[] projection = {MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.DATA,
            MediaStore.Video.Media.SIZE};
    String orderBy = MediaStore.Images.Media.DISPLAY_NAME;
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    return getContentProvider(uri, projection, orderBy);
}

2:获取视频列表

private List<FileVideolModel> getVideosInfo() {

    String[] thumbColumns = new String[]{
            MediaStore.Video.Thumbnails.DATA,
            MediaStore.Video.Thumbnails.VIDEO_ID
    };
    ContentResolver contentResolver = getContentResolver();
    String[] videoColumns = new String[]{
            MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.SIZE,
            MediaStore.Video.Media.MIME_TYPE
    };
    List<FileVideolModel> listImage = new ArrayList<FileVideolModel>();
    FileVideolModel fileVideolModel;
    Cursor cursor = contentResolver.query
            (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null, null);
    while (cursor.moveToNext()) {
        String _id =
                cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
        String filePath =
                cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
        String title =
                cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
        String mime_type =
                cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));

        int SIZE = cursor.getColumnIndex(MediaStore.Video.Media.SIZE);

        Log.i("pony_log", "_id=" + _id);
        Log.i("pony_log", "title=" + title);
        Log.i("pony_log", "filePath=" + filePath);
        Log.i("pony_log", "mime_type=" + mime_type);

        //获取当前Video对应的Id,然后根据该ID获取其Thumb
        String selection = MediaStore.Video.Thumbnails.VIDEO_ID +"=?";
        String[] selectionArgs = new String[]{_id};
        Cursor thumbCursor = this.managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);
        if(thumbCursor.moveToFirst()){
            Log.i("pony_log", "thumbPath=" + cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA)));
        }
        //获取当前Video对应的Id,然后根据该ID获取其Thumb
        int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap infor  = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,  MediaStore.Images.Thumbnails.MICRO_KIND, options);
        fileVideolModel = new FileVideolModel();
        fileVideolModel.setFileName(title);
        fileVideolModel.setFilePath(filePath);
        fileVideolModel.setFileSize(cursor.getString(SIZE));
        fileVideolModel.setFileUrl(infor);
        listImage.add(fileVideolModel);
    }
    return listImage;
}

三、回调显示

private String imagePath;
private String videoPath;
private String imageName;
private String videoName;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==1){
        intentFlag = 1;
        imagePath = data.getStringExtra("msg");
        imageName = data.getStringExtra("name");
        choiceImage.setText(imageName);
   }else{
        intentFlag = 10;
        videoPath = data.getStringExtra("msg");
        videoName = data.getStringExtra("name");
        choiceVideo.setText(videoName);
    
    }
}

留个脚印吧!

大家点赞、收藏、关注、评论啦 ;

希望可以帮助到您;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android毕业设计源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值