【问题标题】:How to save a created bitmap?如何保存创建的位图?
【发布时间】:2015-03-04 00:35:02
【问题描述】:

我想在单击按钮后保存创建的位图
我怎样才能做到这一点?如果可能的话,到一个特定的位置

这是我的代码:

quoteimage.requestLayout();
        if (quoteimage.getViewTreeObserver().isAlive()) {
            quoteimage.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                // we check which build version we are using
                @SuppressLint("NewApi")
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        quoteimage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        quoteimage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }

                    viewToBitmap(quoteimage); ///this one creates the bitmap
                }
            });
        }




        share.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ///Here I want to save the bitmap
            }
        });

【问题讨论】:

  • @NoMoreHelloWorld "你看过了吗"***** 是的,我看过

标签: android bitmap onclick save fileoutputstream


【解决方案1】:

在onClick()中调用save():

protected void save(){
 FileOutputStream out = null;
try {
  out = new FileOutputStream(filename);
  bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
 } catch (Exception e) {
     e.printStackTrace();
 } finally {
 try {
    if (out != null) {
        out.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}
}}

【讨论】:

    【解决方案2】:

    使用此方法保存您的位图,也可以像 U 一样传递文件名和位置

     private createDirectoryAndSaveFile(Bitmap imageToSave, String fileName,String location) {
    
        File direct = new File(Environment.getExternalStorageDirectory() + "/"+location);
    
        if (!direct.exists()) {
            File wallpaperDirectory = new File("/sdcard/"+location+"/");
            wallpaperDirectory.mkdirs();
        }
    
        File file = new File(new File("/sdcard/"+location+"/"), fileName+".jpg");
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        File externalFile = new File(Environment.getExternalStorageDirectory(),"your/location/"+fileName+".jpg");
        externaluri = Uri.parse(externalFile.getPath());
        Log.d("externaluri", externaluri.toString());
    
    }
    

    例子

    createDirectoryAndSaveFile(yourbitmap, "your_image_name" ,"your/location");
    

    【讨论】:

      【解决方案3】:

      Android Saving created bitmap to directory on sd card

      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
      
      //you can create a new file name "test.jpg" in sdcard folder.
      File f = new File(Environment.getExternalStorageDirectory()
                              + File.separator + "test.jpg")
      f.createNewFile();
      //write the bytes in file
      FileOutputStream fo = new FileOutputStream(f);
      fo.write(bytes.toByteArray());
      
      // remember close de FileOutput
      fo.close();
      

      【讨论】:

        猜你喜欢
        • 2017-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-12
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        相关资源
        最近更新 更多