Save scaled image to sdcard android

2019-09-07 02:35发布

问题:

i've the following code, that scale the object. Now i want to save the scaled image to sdcard, please help me regarding that i've searched many but didn't find any

public boolean onTouch(View v, MotionEvent event) {
      ImageView view = (ImageView) v;

      // Dump touch event to log
      dumpEvent(event);

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
         savedMatrix.set(matrix);
         start.set(event.getX(), event.getY());
         Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         break;
      case MotionEvent.ACTION_POINTER_DOWN:
         oldDist = spacing(event);
         Log.d(TAG, "oldDist=" + oldDist);
         if (oldDist > 10f) {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;
            Log.d(TAG, "mode=ZOOM");
         }
         break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
         mode = NONE;
         Log.d(TAG, "mode=NONE");
         break;
      case MotionEvent.ACTION_MOVE:
         if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x,
                  event.getY() - start.y);
         }
         else if (mode == ZOOM) {
            float newDist = spacing(event);
            Log.d(TAG, "newDist=" + newDist);
            if (newDist > 10f) {
               matrix.set(savedMatrix);
               float scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
            }
         }
         break;
      }

      view.setImageMatrix(matrix);
      return true; // indicate event was handled
   }

/** Show an event in the LogCat view, for debugging */

 private void dumpEvent(MotionEvent event) {
      String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
            "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
      StringBuilder sb = new StringBuilder();
      int action = event.getAction();
      int actionCode = action & MotionEvent.ACTION_MASK;
      sb.append("event ACTION_").append(names[actionCode]);
      if (actionCode == MotionEvent.ACTION_POINTER_DOWN
            || actionCode == MotionEvent.ACTION_POINTER_UP) {
         sb.append("(pid ").append(
               action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
         sb.append(")");
      }
      sb.append("[");
      for (int i = 0; i < event.getPointerCount(); i++) {
         sb.append("#").append(i);
         sb.append("(pid ").append(event.getPointerId(i));
         sb.append(")=").append((int) event.getX(i));
         sb.append(",").append((int) event.getY(i));
         if (i + 1 < event.getPointerCount())
            sb.append(";");
      }
      sb.append("]");
      Log.d(TAG, sb.toString());
   }

Any help? Thanks in advance

回答1:

You could do something like this:

public void saveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    view.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
    );

    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.setVisibility(View.VISIBLE);

    Bitmap _b = Bitmap.createBitmap(
        view.getMeasuredWidth(),
        view.getMeasuredHeight(),
        Bitmap.Config.ARGB_8888
    );

    Canvas _c = new Canvas(_b);
    view.draw(_c);

    OutputStream _out = null;
    try {
        _out = new FileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } finally {
        _out.close();
    }
}

The above is heavily modified from some working code I have which produces bitmaps by drawing off-screen. I have not tested it in the form it appears above, and for your needs it may be doing more work than is really necessary.

It occurs to me that the following, shorter version, might work for you, but I have not tested this at all:

public void saveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    Bitmap _b = view.getDrawingCache();
    OutputStream _out = null;
    try {
        _out = new FileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } finally {
        _out.close();
    }
}


回答2:

You can do it, this way

private void saveImage(ImageView view, File file) throws IOException{
        //Before saving image, you need to create a scaled bitmap object 
        //which reflects the actual scaling, rotation, zoom, etc. in itself
          Bitmap src=((BitmapDrawable)view.getDrawable()).getBitmap();
          Bitmap bm= Bitmap.createBitmap(src, 0,0, src.getWidth(), src.getHeight(), matrix, true);

        //Then save this bitmap in a particular file.
          OutputStream out = null;
          try {
            out = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, out);             
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } finally {
            out.close();
          }
    }

This is working code!