Android保存图片和视频到相册

//android把图片文件添加到相册

public String saveImage(String name, Bitmap bmp) {
        File appDir = new File(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM+File.separator+"Camera"+File.separator);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = name + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            try {
                MediaStore.Images.Media.insertImage(getContentResolver(),
                        file.getAbsolutePath(), fileName, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 最后通知图库更新
            Uri localUri = Uri.fromFile(file);

            Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);

            sendBroadcast(localIntent);
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

android把视频文件添加到相册

//是否添加到相册
ContentResolver localContentResolver = this.getContentResolver();
ContentValues localContentValues = getVideoContentValues(this, file, System.currentTimeMillis());
Uri localUri = localContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, localContentValues);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri));
public static ContentValues getVideoContentValues(Context paramContext, File paramFile, long paramLong)
{
  ContentValues localContentValues = new ContentValues();
  localContentValues.put("title", paramFile.getName());
  localContentValues.put("_display_name", paramFile.getName());
  localContentValues.put("mime_type", "video/mp4");
  localContentValues.put("datetaken", Long.valueOf(paramLong));
  localContentValues.put("date_modified", Long.valueOf(paramLong));
  localContentValues.put("date_added", Long.valueOf(paramLong));
  localContentValues.put("_data", paramFile.getAbsolutePath());
  localContentValues.put("_size", Long.valueOf(paramFile.length()));
  return localContentValues;
}

转载自:android把图片 视频 保存到相册

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值