现在我有一个非常简单的应用程序,我可以在其中拍照,然后回答问题。我将图片的文件名捆绑到问题类中,然后使用相同的文件名将问题保存为文本文件(这些都保存在 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
.