如何创建和编辑可包含文本和图像的文件。
我能够保存文件并再次编辑,只有文本。我通过从多行edittext获取文本来做到这一点。我添加了一个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,这是不可编辑的(如果我错了,请纠正我)。
请帮帮我怎样才能做到这一点..
答案 0 :(得分: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());
}
}