2013-07-29 25 views
2

我想設置圖像的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); 
} 

回答

0

你在哪裏讀書ImagePath重新啓動?請顯示相同的代碼。此外,雖然寫的ImagePath到文本文件,你應該做到以下幾點

File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    try { 
     if(!myFile.exists()) { 
      myFile.createNewFile(); 
     } 
     FileOutputStream fOut = new FileOutputStream(myFile); 
     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
     myOutWriter.append(selectedImagePath); 
     myOutWriter.close(); 
     fOut.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

讀取文件時,你也應該閱讀使用Environment.getExternalStorageDirectory()

編輯:

如果你的目的是要公正看看圖片庫,那麼你不應該使用intent.setAction(Intent.ACTION_GET_CONTENT);,將其更改爲intent.setAction(Intent.ACTION_PICK);

現在,下面將是您的文件路徑讀取功能(您的文件編寫代碼將始終覆蓋現有的存儲路徑..因此,您將始終只有一個路徑存儲....如果您希望存儲多個圖像路徑,那麼你應該修改你的文件寫入和下面的文件閱讀部分)。

public String getStoredPath() { 
    String path = null; 
    File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    if(myFile.exists()) { 
     //Read text from file 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(myFile)); 
      String line; 
      // Below while loop will only run once as your file writing 
      // always overwrites the existing line 
      while ((line = br.readLine()) != null) { 
       path = line; 
      } 
     } catch (IOException e) { 

     } 
    } 

    return path; 
} 

然後在你想要設置圖像的地方uri。

String imagePath = getStoredPath(); 
if(imagePath != null) { 
    img.setImageURI(Uri.parse(imagePath)); 
} 

務必對文件寫入進行建議的更改。另外,如果只有一個圖像路徑需要存儲和獲取,請考慮使用共享首選項。

+0

我已經試過這個但沒用,代碼如下 – kirti

+0

代碼在哪裏?你在說'公共字符串getPath(Uri uri)'嗎? – Rajeev

+0

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

相關問題