现在我有一个非常简单的应用程序,我拍照,然后回答一个问题。我有捆绑到问题类的图片的文件名,然后问题被保存为使用相同文件名的文本文件(这些都保存在SD卡上)。现在我要做的是在选择该图片时显示该文本文件(它将在TextView中显示)。所以我的问题是有没有办法在TextView
中显示与所选图像文件同名的文本文件?我无法找到其他类似的东西,虽然看起来这不是一个不寻常的任务。
我的代码如下:
创建图像文件名:
ImageButton button;
static int data = 0;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyDirectory/");
myDir.mkdirs();
if (myDir.exists()) { }
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
text.setText(fname);
File file = new File (myDir, fname);
Uri uriSavedImage = Uri.fromFile(file);
Bundle basket = new Bundle();
basket.putString("key", fname);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Intent is = new Intent(Home.this, Details.class);
is.putExtras(basket);
i.putExtra("output", uriSavedImage);
startActivity(is);
startActivityForResult(i, data);
}
});
创建文本文件名:
textfileName = (TextView)findViewById(R.id.textFile);
Button save;
TextView question;
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
textfileName.setText(gotBread);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Details Saved!", Toast.LENGTH_SHORT).show();
textQuestion= question.getText().toString();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/MyDirectory/");
myDir.mkdirs();
if (myDir.exists()) { }
detailsFile = textfileName.getText().toString() + ".txt";
File file = new File (myDir, detailsFile);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
writer.write(textQuestion);
writer.newLine();
writer.flush();
writer.close();
} catch (IOException e) { e.printStackTrace(); }
}
});
显示选择了图像的文件:
final Button viewBtn = (Button)findViewById(R.id.detailsBtn);
viewBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i<len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please Select One Picture", Toast.LENGTH_SHORT).show();
} else if (cnt > 1) {
Toast.makeText(getApplicationContext(), "Please Select Only One Picture", Toast.LENGTH_SHORT).show();
} else if (cnt == 1) {
setContentView(R.layout.details_text);
detailsView = (TextView)findViewById(R.id.displayDetails);
File dir = Environment.getExternalStorageDirectory();
File myFile = new File(dir, "/MyDirectory/" + "detailsFile-.txt");
if (myFile.exists()) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(myFile));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
}
}
catch (IOException e) {
}
detailsView.setText(text);
}
else
{
Toast.makeText(getApplicationContext(), "File Doesn't Exist", Toast.LENGTH_SHORT).show();
}
}
}
});
我正在else if
中尝试在TextView
中显示文本文件。