2017-04-25 57 views
-1

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

任何人都可以在这里提供任何解决方案?

谢谢! :)

+0

在这里发布您的代码 –

+0

您到目前为止所做的邮政编码,一般的想法是在运行时使用LinearLayout添加'textView'来创建'View',创建'View'的'Bitmap'并保存它到'Environment.getExternalStorageDirectory()' – Vikrant

+0

我什么也没做对。我只是想知道它是如何完成的。我在这个领域的初学者 感谢您的回复.... :) :) 但你会解释一下吗? @Vikrant –

回答

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(),你将来可能需要这个。

+0

@hamza salam,接受答案,如果它帮助你。 – Vikrant