我想設置圖像的ImageView以下列方式如何設置從文本文件映像中的Android
1)當點擊按鈕的圖片庫開放
2)選擇任何圖像,並設定爲圖像視圖&存儲
我做了所有這些工作,但是當應用程序關閉,那些重新開始,該ImageView的不顯示前面的圖像通過從文本文件中檢索圖像路徑
代碼如下的形象路徑文本文件public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
File myFile = new File("/sdcard/ImageLocation.txt");
try {
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(selectedImagePath);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),"Done writing SD ",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
我已經試過這個但沒用,代碼如下 – kirti
代碼在哪裏?你在說'公共字符串getPath(Uri uri)'嗎? – Rajeev
File file = new File(「/ sdcard/ImageLocation.txt」); \t \t if(file。存在()) \t \t { \t \t \t //從文件中讀取文本 \t \t \t StringBuilder的文本=新的StringBuilder(); \t \t \t嘗試{ \t \t \t \t的BufferedReader BR =新的BufferedReader(新的FileReader(文件)); \t \t \t \t絃線; \t \t \t \t而(!(線= br.readLine())= NULL){ \t \t \t \t \t text.append(線); \t \t \t \t} \t \t \t} \t \t \t趕上(IOException的發送){ \t \t \t} \t \t \t \t \t \t \t /// img.setImageURI(selectedImageUri); \t \t \t \t \t // \t我想 \t \t { \t \t \t Toast.makeText(getApplicationContext(),「對不起,文件不分配的形象在這裏 \t \t \t \t \t} \t \t其他存在!!「,Toast.LENGTH_LONG).show(); \t \t} \t} – kirti