我设法使用此代码将两个图像合并为一个,但我不知道如何将其保存到SD卡。我想将它保存到以我的应用名称命名的SD卡中的文件夹。
额外:如果图片名称在时间和日期之后或者是独特的东西之后会很好,所以不会有图像名称的任何副本。
public class CombineImages extends View{
private Bitmap buffer;
private Canvas canvas;
private Matrix matrix = new Matrix();
public CombineImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CombineImages(Context context) {
super(context);
}
public void combine(ImageView imageView){
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
combine(bitmap);
}
public void combine(Bitmap bitmap) {
updateBuffer(bitmap);
draw(canvas);
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(buffer, matrix , null);
}
private void updateBuffer(Bitmap bitmap) {
if(buffer == null){
createBuffer(bitmap);
}
else{
if(bitmap.getWidth() > buffer.getWidth() || bitmap.getHeight() > buffer.getHeight()){
Bitmap oldBuffer = buffer;
createBuffer(bitmap);
drawBitmnapToBuffer(oldBuffer);
oldBuffer.recycle();
}
drawBitmnapToBuffer(bitmap);
}
getLayoutParams().height = buffer.getHeight();
getLayoutParams().width = buffer.getWidth();
}
private void drawBitmnapToBuffer(Bitmap bitmap) {
canvas.save();
// add your translation logic here using canvas.translate(dx, dy);
canvas.drawBitmap(bitmap, matrix, null);
canvas.restore();
}
private void createBuffer(Bitmap bitmap) {
buffer = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
canvas = new Canvas(buffer);
}
}
答案 0 :(得分:1)
您可以使用以下方法将新生成的位图保存到SD卡。希望这有帮助!
public Uri saveToSD(Bitmap photo) {
File picFile;
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
picFile = new File(root, generateFileName());
FileOutputStream out = new FileOutputStream(picFile);
photo.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
return Uri.fromFile(picFile);
}
public String generateFileName() {
Time now = new Time();
now.setToNow();
return "MyAppName_" + now.toString() + ".png";
}
答案 1 :(得分:0)
首先,确保您的应用程序对SD卡具有写权限,清单应具有以下内容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
其次,您应该检查是否有可用的外部存储(SD卡)。 (见这里:https://developer.android.com/guide/topics/data/data-storage.html#filesExternal)
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
第三,将文件写入您选择的目录,类似于下面的内容。创建一个文件,使用输出缓冲区写入或者在imageview的情况下(参见这里:https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29):
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);//what ever directory you want, this example pictures directory or above code also works
Date date = new Date();
String fileName = "DemoPicture" + date.getTime() + ".jpg";//or how ever you want to format the name
File file = new File(path, fileName);
try {
// Make sure the Pictures directory exists.
path.mkdirs();
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
最后,如果这仍然没有削减它...谷歌是你的朋友:(见这里:http://www.brighthub.com/mobile/google-android/articles/64048.aspx)