我正在研究从web服务器下载图像的功能,在屏幕上显示它,如果用户希望保持图像,将其保存在SD卡的某个文件夹中。有没有一种简单的方法来获取位图并将其保存到我选择的SD卡文件夹中?
我的问题是,我可以下载图像,在屏幕上显示为位图。我能够找到的将图像保存到特定文件夹的唯一方法是使用FileOutputStream,但这需要一个字节数组。我不知道如何转换(如果这是正确的方式)从位图到字节数组,所以我可以使用FileOutputStream写入数据。
我的另一个选择是使用MediaStore:
MediaStore.Images.Media.insertImage(getContentResolver(), bm,
barcodeNumber + ".jpg Card Image", barcodeNumber + ".jpg Card Image");
它可以很好地保存到SD卡,但不允许您自定义文件夹。
我还想保存一张图片。但是我的问题(?)是我想从我画的位图中保存它。
我是这样写的:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_sign:
myView.save();
break;
}
return false;
}
public void save() {
String filename;
Date date = new Date(0);
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
filename = sdf.format(date);
try{
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "/DCIM/Signatures/"+filename+".jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver()
,file.getAbsolutePath(),file.getName(),file.getName());
}catch (Exception e) {
e.printStackTrace();
}
}
Bitmap bbicon;
bbicon=BitmapFactory.decodeResource(getResources(),R.drawable.bannerd10);
//ByteArrayOutputStream baosicon = new ByteArrayOutputStream();
//bbicon.compress(Bitmap.CompressFormat.PNG,0, baosicon);
//bicon=baosicon.toByteArray();
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, "er.PNG");
try {
outStream = new FileOutputStream(file);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch(Exception e) {
}
我发现发送PNG和透明度的方式。
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/CustomDir";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
String format = new SimpleDateFormat("yyyyMMddHHmmss",
java.util.Locale.getDefault()).format(new Date());
File file = new File(dir, format + ".png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
yourbitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent,"Sharing something")));
一些新设备不保存位图,所以我解释了一点。
请确保您在下面添加了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并在XML文件夹名为provider_paths.xml下创建一个XML文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
and in AndroidManifest under
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
then simply call saveBitmapFile(passYourBitmapHere)
public static void saveBitmapFile(Bitmap bitmap) throws IOException {
File mediaFile = getOutputMediaFile();
FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, getQualityNumber(bitmap), fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
where
File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(),
"easyTouchPro");
if (mediaStorageDir.isDirectory()) {
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(Calendar.getInstance().getTime());
String mCurrentPath = mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg";
File mediaFile = new File(mCurrentPath);
return mediaFile;
} else { /// error handling for PIE devices..
mediaStorageDir.delete();
mediaStorageDir.mkdirs();
galleryAddPic(mediaStorageDir);
return (getOutputMediaFile());
}
}
and other methods
public static int getQualityNumber(Bitmap bitmap) {
int size = bitmap.getByteCount();
int percentage = 0;
if (size > 500000 && size <= 800000) {
percentage = 15;
} else if (size > 800000 && size <= 1000000) {
percentage = 20;
} else if (size > 1000000 && size <= 1500000) {
percentage = 25;
} else if (size > 1500000 && size <= 2500000) {
percentage = 27;
} else if (size > 2500000 && size <= 3500000) {
percentage = 30;
} else if (size > 3500000 && size <= 4000000) {
percentage = 40;
} else if (size > 4000000 && size <= 5000000) {
percentage = 50;
} else if (size > 5000000) {
percentage = 75;
}
return percentage;
}
and
void galleryAddPic(File f) {
Intent mediaScanIntent = new Intent(
"android.intent.action.MEDIA_SCANNER_SCAN_FILE");
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}