我怎么能保存的ImageView的在Android的位图的内容? [英] How can I save the content of an ImageView to a Bitmap in Android?

查看:99
本文介绍了我怎么能保存的ImageView的在Android的位图的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了与显示用户绘制的位图,万事如意一个ImageView的应用程序,但现在我想给可能给用户从ImageView的图像保存为JPEG文件,当我点击上的按钮。
我怎样才能做到这一点?

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?

下面是我的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);
    }

在code创建文件夹并保存图像的工作,因为我可以找到myPainting文件夹中的文件image.jpg的,但图像的内容完全是黑色的,无需用户绘制的图像。
我怎样才能获得的ImageView的内容? (而以位图格式,然后其他的事情)

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)

推荐答案

ImageView的内部使用 ScaleType
它会根据屏幕的像素缩放你的形象,如果你想获得图像的原始质量,使用绘图缓存,

Imageview internally use ScaleType. it will scale your image according the the pixels of the screen,if you want to get the original quality of the image,use drawing cache,

  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();
   }

这篇关于我怎么能保存的ImageView的在Android的位图的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆