保存图像并将文本编辑到图库中

时间:2017-04-25 06:55:58

标签: android text save gallery android-studio-2.2

我正在尝试创建一个在图像上添加文本的应用程序,如图像编辑器并将其保存到库中。在这个我的图像来自画廊或相机,文字是由用户添加但问题是图像上的文字没有存储到画廊

有人可以提供任何解决方案吗?

谢谢! :)

1 个答案:

答案 0 :(得分:0)

尝试类似:

1)创建一个Linearlayout

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout_you_need);

2)添加TextView

    TextView tv = new TextView(MainActivity.this);
    //tv.setText("Whatever you like");
    layout.addView(tv); // add TextView on runtime

    layout.setDrawingCacheEnabled(true);

    layout.buildDrawingCache(true);

    Bitmap saveBm = Bitmap.createBitmap(layout.getDrawingCache());

    layout.setDrawingCacheEnabled(false);

    SaveImage(savebm); 

3)保存位图

    private void SaveImage(Bitmap finalBitmap) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

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

另外请理解your_layout_view.invalidate()的需要,将来可能还需要这个。

相关问题