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把图片 视频 保存到相册


版权声明:本文为llm20143304原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。