我有一个问题如何将我的App中的位图保存到Button中的android库中?我只是希望我的位图放在图库中,用户可以使用它将其设置为可能是他的背景。那怎么回事?
答案 0 :(得分:0)
您可以修改以下功能以满足您的需求
private void takeSnapShot(Bitmap bitmap) {
OutputStream outStream = null;
String state = Environment.getExternalStorageState();
String appName = mContext.getString(R.string.app_name);
if (state.equals(Environment.MEDIA_MOUNTED)) {
String sdroot = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = sdroot + "/" + appName;
File dir = new File(path);
if (!dir.exists() && !dir.mkdirs()) {
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
String dstr = sdf.format(new Date());
String filename = path + "/" + dstr + ".jpg";
try {
outStream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
LogUtils.println("Exception while writing image");
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
try {
if (outStream != null)
outStream.close();
mMediaScanner.scanMediaByName(filename, R.string.snapshot_save);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Add some parameters to the image that will be stored in the Image ContentProvider
int UNIQUE_BUCKET_ID = 111;
String disp = "anything";
ContentValues values = new ContentValues(7);
values.put(MediaStore.Images.Media.DISPLAY_NAME, disp);
values.put(MediaStore.Images.Media.TITLE, disp);
values.put(MediaStore.Images.Media.DESCRIPTION, appName + disp);
values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME, appName + " snapshot");
values.put(MediaStore.Images.Media.BUCKET_ID, UNIQUE_BUCKET_ID);
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
// Inserting the image meta data inside the content provider
Uri uri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
if (uri != null) {
outStream = mContext.getContentResolver().openOutputStream(uri);
if (outStream != null) {
outStream.close();
}
}
} catch (FileNotFoundException e) {
LogUtils.println("Exception while writing image");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
您可以像这样在图库中保存图片。
MediaStore.Images.Media.insertImage(getContentResolver(),yourBitmap,yourTitle,yourDescription);
试试这个
图像显示在图库中,存储在媒体商店提供商
上public static void addImageToGallery(final String filePath,final Context context){
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}