Android显示与图像文件同名的文本文件
现在我有一个非常简单的应用程序,我可以拍照,然后回答问题。我将图片的文件名绑定到问题类,然后使用相同的文件名将问题保存为文本文件(这些文件都保存在SD卡上)。现在我要做的是在选中图片时显示该文本文件(它将显示在文本视图中)。所以我的问题是,有没有一种方法可以在Android显示与图像文件同名的文本文件,android,image,file,text,filenames,Android,Image,File,Text,Filenames,现在我有一个非常简单的应用程序,我可以拍照,然后回答问题。我将图片的文件名绑定到问题类,然后使用相同的文件名将问题保存为文本文件(这些文件都保存在SD卡上)。现在我要做的是在选中图片时显示该文本文件(它将显示在文本视图中)。所以我的问题是,有没有一种方法可以在TextView中显示与所选图像文件同名的文本文件?我还没有找到其他类似的东西,尽管这似乎不是一个不寻常的任务 我的代码如下: 正在创建图像文件名: ImageButton button; static int data = 0; but
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(); }
}
});
创建文本文件名:
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();
}
}
}
});
final Button viewBtn=(按钮)findViewById(R.id.detailsBtn);
viewBtn.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
final int len=缩略图selection.length;
int-cnt=0;
字符串selectImages=“”;
对于(int i=0;i 1){
Toast.makeText(getApplicationContext(),“请仅选择一张图片”,Toast.LENGTH\u SHORT.show();
}else if(cnt==1){
setContentView(R.layout.details\u text);
detailsView=(TextView)findViewById(R.id.displayDetails);
File dir=Environment.getExternalStorageDirectory();
File myFile=新文件(dir,“/MyDirectory/”+“detailsFile-.txt”);
如果(myFile.exists()){
StringBuilder text=新的StringBuilder();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(myFile));
弦线;
而((line=br.readLine())!=null){
text.append(行);
}
}
捕获(IOE异常){
}
detailsView.setText(文本);
}
其他的
{
Toast.makeText(getApplicationContext(),“文件不存在”,Toast.LENGTH\u SHORT.show();
}
}
}
});
在
else if
中,我试图在文本视图中显示文本文件
是。有一种方法可以做到这一点。如果你发布你的代码,我相信有人可以帮你。好的,我编辑了我的问题并提供了代码。非常感谢。有没有任何建议,任何人都可以抛弃我?我一直在努力,但是没有用。挫折正在踢我的胯部!!