1

如何创建和编辑可以包含文本和图像的文件。

我能够保存一个文件并再次编辑它,它只有文本。我通过从多行编辑文本中获取文本来做到这一点。我添加了一个 imageview 并为其设置了一个图像。但我不知道如何保存并检索以进行编辑。

      public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson_edit);

txtData = (EditText) findViewById(R.id.txtData);
img =(ImageView)findViewById(R.id.imageView1);

final String path = "/sdcard/ram/notebook/lesson";

try {
        FileInputStream fIn = new FileInputStream(path);
        BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
        String aDataRow = "";
        String aBuffer = "";
        while ((aDataRow = myReader.readLine()) != null) {
            aBuffer += aDataRow + "\n";
        }
        txtData.setText(aBuffer);
        myReader.close();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
    }

btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    String notes = txtData.getText().toString()+ img.getBackground();
    try {
          FileOutputStream fOut = new FileOutputStream(path);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            myOutWriter.append(notes);
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(),"Done writing",Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
        }
      }// onClick
}); // btnWriteSDFile
}

我尝试过使用画布。但它将文本和图像保存为 jpg,这是不可编辑的(如果我错了,请纠正我)。

请帮助我如何做到这一点..

4

1 回答 1

0

我已经通过下面的代码实现了这一点。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sv = new ScrollView(getApplicationContext());
    ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setBackgroundColor(Color.CYAN);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    sv.addView(ll);
    setContentView(sv);
Button b1 = new (Button)findViewById(R.id.button1);
b1..setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
saveAsJpg();
            }
        });

}
public void saveAsJpg ()
{
    //Bitmap b = Bitmap.createBitmap(sv.getWidth(), sv.getHeight(), Bitmap.Config.ARGB_8888);
    sv.setDrawingCacheEnabled(true);
    Bitmap b = sv.getDrawingCache();
      try {
          b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/sdcard/ram/ert/d.jpg"));
          Toast.makeText(this, "Saved!", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
          Log.v("EXCEPTION", e.getMessage());
        }

}
于 2013-01-09T08:57:07.413 回答