android将画布保存为jpg图像

时间:2014-02-20 12:46:40

标签: android image canvas save jpeg

我有这个代码将画布保存到图像中。这里是Oncreate中的函数 但是保存的图像只有1,一旦我将另一个画布保存到图像,它只会覆盖第一个。请帮助我。谢谢

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    mTempDir =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + "PixiePhotos" + "/";
    mCurrent = "PXD_" + timeStamp + ".jpg";
    prepareDirectory();



      save.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {try {
                mBitmapDrawable = new BitmapDrawable(viewBitmap);
                mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();
                String FtoSave = mTempDir + mCurrent;
                File mFile = new File(FtoSave);
                mFileOutputStream = new FileOutputStream(mFile);
                mNewSaving.compress(CompressFormat.JPEG, 100, mFileOutputStream);
                mFileOutputStream.flush();
                mFileOutputStream.close();
              } catch (FileNotFoundException e) {
                Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
              } catch (IOException e) {
                Log.v(TAG, "IOExceptionError " + e.toString());
              }

            }
          });
    }

  private boolean prepareDirectory() {
        try {
          if (makeDirectory()) {
            return true;
          } else {
            return false;
          }
        } catch (Exception e) {
          e.printStackTrace();
          //Toast.makeText(this, getString(R.string.sdcard_error), 1000).show();
          return false;
        }
      }

    private boolean makeDirectory() {
        File mTempFile = new File(mTempDir);
        if (!mTempFile.exists()) {
          mTempFile.mkdirs();
        }

        if (mTempFile.isDirectory()) {
          File[] mFiles = mTempFile.listFiles();
          for (File mEveryFile : mFiles) {
            if (!mEveryFile.delete()) {
              //System.out.println(getString(R.string.failed_to_delete) + mEveryFile);
            }
          }
        }
        return (mTempFile.isDirectory());
      }

2 个答案:

答案 0 :(得分:0)

我认为你只获得一次时间戳,每次点击保存都应该记录。

save.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {try {
            mBitmapDrawable = new BitmapDrawable(viewBitmap);
            mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();

            mCurrent = "PXD_" + new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date()); + ".jpg";

            String FtoSave = mTempDir + mCurrent;
            File mFile = new File(FtoSave);
            mFileOutputStream = new FileOutputStream(mFile);
            mNewSaving.compress(CompressFormat.JPEG, 100, mFileOutputStream);
            mFileOutputStream.flush();
            mFileOutputStream.close();
          } catch (FileNotFoundException e) {
            Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
          } catch (IOException e) {
            Log.v(TAG, "IOExceptionError " + e.toString());
          }

        }
      });

答案 1 :(得分:0)

在点击内设置String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());mCurrent = "PXD_" + timeStamp + ".jpg";,因为创建活动时只会调用一次oncreate。