1

I've made an application with an ImageView that displays a Bitmap drawn by the user and everything goes well, but now I want to give the possibility to the user to save the image from the ImageView to a jpeg file when I click on a button. How can I do this?

Here is my code:

    ImageView imageview;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageview=(ImageView)findViewById(R.id.imageviewcomponent);
        imageview.setDrawingCacheEnabled(true);
    }

    //This method sets the Bitmap into the ImageView from the ActivityResult of the Drawing Activity
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == get_code && resultCode == RESULT_OK)
    {
        iv.setImageBitmap((Bitmap) data.getParcelableExtra("Painting"));
        }
    }

    //This is the onClick method of the button for saving the image
    public void SaveAsImage(View btsave) throws FileNotFoundException {
        Bitmap b=Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(),        Bitmap.Config.ARGB_8888);
        OutputStream fOut = null;
        new File("/sdcard/signatures").mkdir();
        fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
        b.compress(CompressFormat.JPEG, 95, fOut);
    }

The code for creating the folder and save the image is working, because I can find an image.jpg file in the myPainting folder, but the image's content is completely black, without the image drawn by the user. How can I get the content of the ImageView? (rather in Bitmap format then other things)

4

3 に答える 3

1

このコードを試して、何が起こるか教えてください。

//This is the onClick method of the button for saving the image
    public void SaveAsImage(View btsave) throws FileNotFoundException {

        Bitmap b = Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(),        Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(b);
        imageview.draw(canvas);
        OutputStream fOut = null;
        new File("/sdcard/signatures").mkdir();
        fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
        b.compress(CompressFormat.JPEG, 95, fOut);

}
于 2012-06-18T09:48:22.030 に答える
0

user370305 コードのわずかに変更されたバージョンも機能するはずです

static void saveDrawable(ImageView imageView) throws FileNotFoundException{
        Drawable drawable = imageView.getDrawable();
        Rect bounds = drawable.getBounds();
        Bitmap bitmap = Bitmap.createBitmap(bounds.width(),bounds.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        OutputStream fOut = null;
        try {
            new File("/sdcard/signatures").mkdir();
            fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
        } finally {
            if ( fOut != null ){
                try {
                    fOut.close();
                } catch (IOException e) {
                    //report error
                }
            }

        }
    }
于 2012-06-18T09:57:11.297 に答える
0

Imageview は内部で ScaleType を使用します。画面のピクセルに応じて画像をスケーリングします。画像の元の品質を取得したい場合は、描画キャッシュを使用します。

  public void SaveAsImage(Holder imageview) throws FileNotFoundException {

    View view=findViewById(R.id.holder_frame);
    Bitmap bmp=viewToBitmap(imageview);
    imageview.setDrawingCacheEnabled(true);
    imageview.buildDrawingCache(true);


   Bitmap bitmap1=imageview.getDrawingCache();
    OutputStream fOut = null;

    new File("/sdcard/Pic").mkdir();
    fOut = new FileOutputStream("/sdcard/Pic/image4.jpg");
    bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

    imageview.setDrawingCacheEnabled(false);
    imageview.destroyDrawingCache();
   }
于 2015-03-29T19:16:21.880 に答える