Right now I have a very simple app where I take a picture, then answer a question. I have the file name for the picture bundled over to the question class, and then the question is saved as a Text file using that same file name (these are all saved on the SD card). Now what I am trying to do is to display that Text File (it'll be displayed in a TextView) when that picture is selected. So my question would be is there a way to display a Text File that shares the same name as a selected Image file in a TextView
? I haven't been able to find anything else out there that does something like this, though it seems like this wouldn't be an uncommon task.
My Code is below:
Creating Image File Name:
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);
}
});
Creating Text File Name:
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(); }
}
});
Displaying File with Image Selected:
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();
}
}
}
});
It is in the else if
that I am trying to display the Text File in the TextView
.