馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
我現在有個action_pick的程式,取得取得該檔案的 uri 但是不知道要怎麼回傳給上面宣告的private String srcPath ;
然後用textview去讀取srcPath這個字串,mText1.setText("文件路径:\n" + srcPath);- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class DatapickActivity extends Activity {
- /** Called when the activity is first created. */
- private TextView mText1;
- private String srcPath ;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mText1 = (TextView) findViewById(R.id.myText2);
- mText1.setText("文件路径:\n" + srcPath);
-
- Button b = (Button)this.findViewById(R.id.buttonObj);
-
- b.setOnClickListener( new OnClickListener(){
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
-
- // 建立 "選擇檔案 Action" 的 Intent
- Intent intent = new Intent( Intent.ACTION_PICK );
-
- // 過濾檔案格式
- intent.setType( "image/*" );
-
- // 建立 "檔案選擇器" 的 Intent (第二個參數: 選擇器的標題)
- Intent destIntent = Intent.createChooser( intent, "選擇檔案" );
-
- // 切換到檔案選擇器 (它的處理結果, 會觸發 onActivityResult 事件)
- startActivityForResult( destIntent, 0 );
- }
- });
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
-
- // 有選擇檔案
- if ( resultCode == RESULT_OK )
- {
- // 取得檔案的 Uri
- Uri uri = data.getData();
-
-
- if( uri != null )
- {
- // 利用 Uri 顯示 ImageView 圖片
- ImageView iv = (ImageView)this.findViewById(R.id.imageViewObj);
- iv.setImageURI( uri );
-
-
- }
- else
- {
- setTitle("無效的檔案路徑 !!");
- }
- }
- else
- {
- setTitle("取消選擇檔案 !!");
- }
- }
- }
複製代碼 |
|