Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to Save a PictureDrawable as a JPEG/PNG file in Android?

How to Save a PictureDrawable as a JPEG/PNG file in Android?

Scheduled Pinned Locked Moved Java
csharpandroidgraphicstutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bosah Chude
    wrote on last edited by
    #1

    I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work

    PictureDrawable myDrawable = GetPictureDrawable();
    Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    myDrawable.draw(new Canvas(bitmap));
    bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));

    What am I doing wrongly?

    www.belfox.net ...Simplicity at its best

    T X 2 Replies Last reply
    0
    • B Bosah Chude

      I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work

      PictureDrawable myDrawable = GetPictureDrawable();
      Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
      myDrawable.draw(new Canvas(bitmap));
      bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));

      What am I doing wrongly?

      www.belfox.net ...Simplicity at its best

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      as PictureDrawable can be instanated usind a picture, it should be fairly possible to convert it back. 2D Graphics @ android Devguide[^] Documentation on android.graphics.PictureDrawable[^] That one states a function getPicture() that returns a Picture Documentation on android.graphics.Picture[^] that one has a function writeToStream() - should be worth some try? regards Torsten

      I never finish anyth...

      1 Reply Last reply
      0
      • B Bosah Chude

        I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work

        PictureDrawable myDrawable = GetPictureDrawable();
        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
        myDrawable.draw(new Canvas(bitmap));
        bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));

        What am I doing wrongly?

        www.belfox.net ...Simplicity at its best

        X Offline
        X Offline
        xmxkkk
        wrote on last edited by
        #3

        public static void savePic(Bitmap bitmap, String saveFilePath) throws Exception {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFilePath));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        }

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

        add in the file "AndroidManifest.xml" 3.

        Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.g7);
        try {
        savePic(bitmap, "/mnt/sdcard/test.jpg");
        } catch (Exception e) {
        e.printStackTrace();
        }

        modified on Friday, June 3, 2011 1:22 AM

        B 1 Reply Last reply
        0
        • X xmxkkk

          public static void savePic(Bitmap bitmap, String saveFilePath) throws Exception {
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFilePath));
          bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
          bos.flush();
          bos.close();
          }

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

          add in the file "AndroidManifest.xml" 3.

          Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.g7);
          try {
          savePic(bitmap, "/mnt/sdcard/test.jpg");
          } catch (Exception e) {
          e.printStackTrace();
          }

          modified on Friday, June 3, 2011 1:22 AM

          B Offline
          B Offline
          Bosah Chude
          wrote on last edited by
          #4

          Your answer shows how to save a Bitmap to a the SDCard. However, my question was how to save a PictureDrawable to the memory card. I needed to know how to convert the PictureDrawable to a Bitmap.

          www.belfox.net ...Simplicity at its best

          X 1 Reply Last reply
          0
          • B Bosah Chude

            Your answer shows how to save a Bitmap to a the SDCard. However, my question was how to save a PictureDrawable to the memory card. I needed to know how to convert the PictureDrawable to a Bitmap.

            www.belfox.net ...Simplicity at its best

            X Offline
            X Offline
            xmxkkk
            wrote on last edited by
            #5

            private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
            Bitmap bitmap=Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
            Canvas canvas=new Canvas(bitmap);
            canvas.drawPicture(pictureDrawable.getPicture());
            return bitmap;
            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups