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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio中保存图片相册可以通过以下步骤进行: 1. 首先,确保你的应用已经申请了写入外部存储的权限。在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 创建一个用于保存图片的方法,如下所示: ```java private void saveImageToGallery(Bitmap bitmap) { // 获取存储路径 String savePath = Environment.getExternalStorageDirectory().getPath() + "/YourAppName/"; // 创建文件夹 File appDir = new File(savePath); if (!appDir.exists()) { appDir.mkdirs(); } // 生成文件名 String fileName = System.currentTimeMillis() + ".jpg"; // 创建文件对象 File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); // 将bitmap保存到文件中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } // 发送广播通知系统图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(file); intent.setData(uri); sendBroadcast(intent); } ``` 3. 调用该方法来保存图片相册: ```java Bitmap bitmap = ...; // 你要保存的图片Bitmap对象 saveImageToGallery(bitmap); ``` 这样就能将图片保存相册中了。请注意,Android 10及以上版本需要额外处理,以适配分区存储的限制。这里提供的代码示例适用于Android 10以下的版本。如需适配Android 10及以上版本,请参考官方文档并进行相应修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值