2

My requirement is,

  1. Load an image for drawable folder then add round corner around the image.
  2. Fill with White/Green color Stroke around the image.

Here i complete both of them, here my problem is, when i store the image which is displayed in the image view it doesn't stored.

For round corner i used the below code.

   private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

        Bitmap bitmap = scaledBitmap.getBitmap();

        result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);

        color = 0xff424242;
        paint = new Paint();
        rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        roundPx = i;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

               //Add Stroke code Snippet here

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        BitmapDrawable finalresult = new BitmapDrawable(result);
        saveImage_Rotate_white(finalresult.getBitmap());
        return finalresult;
    }

in my main.xml:

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/finalLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/roundcorner"
        android:src="@drawable/test" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:text="Click here to Rotate" />

</RelativeLayout>

To make ImageView as rounded edge,

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="6dp"
        android:color="#FFFFFF" />

    <padding
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp" />

    <corners android:radius="12dp" />

</shape>

The final result of the code looks like below

enter image description here

Here i must save this image in my SdCard. How can i do this. Instead of xml, i tried with below code snippet in roundCornered() method. This also won't work.

          // for stroke

     final Rect rect1 = new Rect((rect.left - 10), (rect.top - 10),
     (rect.right + 10), (rect.bottom + 10));
     final RectF rectF1 = new RectF(rect1);

     Paint p = new Paint();
     p.setStrokeWidth(1.0f);
     p.setColor(Color.GREEN);
     p.setStyle(Style.STROKE);
     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
     canvas.drawRoundRect(rectF1, 12, 12, p);

My Whole Activity is,

public class BorderDraw extends Activity {
    ImageView rec_image;
    String rec_path;
    RelativeLayout mLayout;
    private Bitmap result;
    private int color;
    private Paint paint;
    private Rect rect;
    private RectF rectF;
    private Canvas canvas;
    private float roundPx;
    RelativeLayout innerLayout;
    static File rotated_File, bordered_file;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button) findViewById(R.id.button1);
        b1.setVisibility(View.GONE);
        mLayout = (RelativeLayout) findViewById(R.id.finalLayout);
        rec_image = (ImageView) findViewById(R.id.imageView1);
        rec_path = getIntent().getExtras().getString("image_file");
        Log.v("Image Path", rec_path);
        // rec_image.setDrawingCacheEnabled(true);
        String fname = "RoundCorner.jpg";
        rotated_File = new File("/sdcard/" + fname);
        bordered_file = new File("/sdcard/cornered_image.png");
        BitmapDrawable scaledBitmap = resizeBitmap(rec_path, 200, 200,
                rec_image);

        BitmapDrawable cornered_bitmap = roundCornered(scaledBitmap, 12);

        // saveImage_Rotate_white(cornered_bitmap.getBitmap());
        rec_image.setImageDrawable(cornered_bitmap);

        System.out.println("ImageView Height" + rec_image.getHeight());
        System.out.println("ImageView Width" + rec_image.getWidth());

    }

    private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

        Bitmap bitmap = scaledBitmap.getBitmap();

        result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);

        color = 0xff424242;
        paint = new Paint();
        rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        roundPx = i;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        BitmapDrawable finalresult = new BitmapDrawable(result);
        saveImage_Rotate_white(finalresult.getBitmap());
        return finalresult;
    }

    private void saveImage_Rotate_white(Bitmap bitmap) {
        rec_image.setImageBitmap(bitmap);
        final Bitmap myRoundedImage = rec_image.getDrawingCache();

        if (bordered_file.exists())
            bordered_file.delete();
        try {
            FileOutputStream out = new FileOutputStream(bordered_file);
            myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private BitmapDrawable resizeBitmap(String rec_path2, int wantedWidth,
            int wantedHeight, ImageView view) {
        // TODO Auto-generated method stub
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16 * 1024];

        Bitmap rec_bitmap = BitmapFactory.decodeFile(rec_path2, options);

        int width = rec_bitmap.getWidth();
        int height = rec_bitmap.getHeight();
        float xScale = ((float) wantedWidth) / width;
        float yScale = ((float) wantedHeight) / height;
        float scale = (xScale <= yScale) ? xScale : yScale;
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap scaledBitmap = Bitmap.createBitmap(rec_bitmap, 0, 0, width,
                height, matrix, true);
        BitmapDrawable finalImage = new BitmapDrawable(scaledBitmap);
        width = scaledBitmap.getWidth();
        height = scaledBitmap.getHeight();

        return finalImage;

    }

    static void saveImage_Rotate(Bitmap dest2) {

        if (rotated_File.exists())
            rotated_File.delete();
        try {
            FileOutputStream out = new FileOutputStream(rotated_File);
            dest2.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
Aerrow
  • 12,086
  • 10
  • 56
  • 90

2 Answers2

0

well one thing you could implement is the following: In your Activity, Fragment, or wherever you are inflating your XML enable the cache for your View like this:

ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
myImageView.setDrawingCacheEnabled(true);

and whenever you want to retrieve the bitmap you just call the get drawing cache like this:

final Bitmap myRoundedImage = myImageView.getDrawingCache();

Alternatively you can do this:

final Bitmap myRoundedImage = Bitmap.createBitmap( 
    myImageView.getWidth(), myImageView.getHeight(), Bitmap.Config.ARGB_8888 ); 
final Canvas canvas = new Canvas( myRoundedImage ); 
myImageView.draw( canvas );

and once you have your bitmap, all you have to do is save it to the SD card with something like:

try {
       FileOutputStream out = new FileOutputStream(filename);
       myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
       e.printStackTrace();
}
Raykud
  • 2,488
  • 3
  • 21
  • 41
  • I used both ways but i couldn't get my solution. When i use this final Bitmap myRoundedImage = myImageView.getDrawingCache(); the image won't create. Device say's "Couldn't load this image" as well as when i use final Bitmap myRoundedImage = Bitmap.createBitmap( myImageView.getWidth(), myImageView.getHeight(), Bitmap.Config.ARGB_8888 ); final Canvas canvas = new Canvas( myRoundedImage ); myImageView.draw( canvas ); means it throws error on this line. – Aerrow Jun 13 '12 at 17:37
  • for me to edit your code I would require a bit more of code. Like your Activity or fragment or wherever you are declaring your ImageView – Raykud Jun 13 '12 at 23:21
  • i added my full activity kindly notice it – Aerrow Jun 14 '12 at 08:09
  • Hi it works, when i place the code inside the button click.. Thanks for your effort – Aerrow Jun 18 '12 at 12:47
0
Bitmap b = Bitmap.createBitmap(iv_cover.getWidth(), iv_cover.getHeight(),Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(b);
                    iv_cover.draw(canvas);
                    OutputStream fOut = null;
                    try {
                        fOut = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+ File.separator + "image.jpg");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    b.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30