Android 合并两个imageview后保存到SD卡

Android 合并两个imageview后保存到SD卡,android,android-imageview,android-external-storage,Android,Android Imageview,Android External Storage,我设法用这段代码将两个图像合并成一个,但我不知道如何将其保存到sd卡。我想将其保存到sd卡中以我的应用程序名命名的文件夹中 额外:如果图像名称在时间和日期之后或者是唯一的,那么就不会有任何图像名称的副本 public class CombineImages extends View{ private Bitmap buffer; private Canvas canvas; private Matrix matrix = new Matrix()

我设法用这段代码将两个图像合并成一个,但我不知道如何将其保存到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);
        }
    } 

您可以使用以下方法将新生成的位图保存到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";
  }

首先,确保应用程序具有SD卡的写入权限,清单应具有以下内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
第三,将文件写入所选目录,如下所示。创建一个文件,使用输出缓冲区写入,或者对于imageview(请参见此处:):

最后,如果这仍然不能解决问题。。。谷歌是你的朋友:(见这里:)

/* 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;
}
    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);
    }