綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 要怎麼把取得的uri 回傳給String srcPath呢?

[複製連結] 查看: 1233|回覆: 6|好評: 0
跳轉到指定樓層
樓主
wind8894 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2012-7-19 18:47

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
我現在有個action_pick的程式,取得取得該檔案的 uri 但是不知道要怎麼回傳給上面宣告的private String srcPath ;
然後用textview去讀取srcPath這個字串,mText1.setText("文件路径:\n" + srcPath);
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;

  10. public class DatapickActivity extends Activity {
  11.     /** Called when the activity is first created. */
  12.         private TextView mText1;
  13.         private String srcPath ;
  14.    
  15.        
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.main);
  20.         
  21.         mText1 = (TextView) findViewById(R.id.myText2);
  22.         mText1.setText("文件路径:\n" + srcPath);
  23.         
  24.         Button b = (Button)this.findViewById(R.id.buttonObj);
  25.         
  26.         b.setOnClickListener( new OnClickListener(){
  27.             public void onClick(View arg0) {
  28.                 // TODO Auto-generated method stub
  29.                
  30.                 // 建立 "選擇檔案 Action" 的 Intent
  31.                 Intent intent = new Intent( Intent.ACTION_PICK );
  32.                
  33.                 // 過濾檔案格式
  34.                 intent.setType( "image/*" );
  35.                
  36.                 // 建立 "檔案選擇器" 的 Intent  (第二個參數: 選擇器的標題)
  37.                 Intent destIntent = Intent.createChooser( intent, "選擇檔案" );
  38.                
  39.                 // 切換到檔案選擇器 (它的處理結果, 會觸發 onActivityResult 事件)
  40.                 startActivityForResult( destIntent, 0 );
  41.             }
  42.         });
  43.     }
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  45.         
  46.         // TODO Auto-generated method stub
  47.         super.onActivityResult(requestCode, resultCode, data);
  48.         
  49.         // 有選擇檔案
  50.         if ( resultCode == RESULT_OK )
  51.         {
  52.             // 取得檔案的 Uri
  53.             Uri uri = data.getData();
  54.             
  55.             
  56.             if( uri != null )
  57.             {
  58.                 // 利用 Uri 顯示 ImageView 圖片
  59.                 ImageView iv = (ImageView)this.findViewById(R.id.imageViewObj);
  60.                 iv.setImageURI( uri );
  61.                
  62.                
  63.             }
  64.             else
  65.             {
  66.                 setTitle("無效的檔案路徑 !!");
  67.             }
  68.         }
  69.         else
  70.         {
  71.             setTitle("取消選擇檔案 !!");
  72.         }
  73.     }
  74. }
複製代碼
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2012-7-20 15:45
intent 可以傳遞參數
putExtra("Name", "Value")

在另一個 activity 取值
getIntent().getStringExtra("Name")

型態請自行變換
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-7-20 16:20
他好像是在同一個activity吧?只是action_pick使得按下button後會跳出選擇圖片的activity
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
ploglin | 收聽TA | 只看該作者
發表於 2012-7-21 08:32
如果是這支程式,根本就沒有給值呀?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-7-21 11:10
本帖最後由 wind8894 於 2012-7-21 11:15 編輯

因為uri 是檔案的路徑 要選擇SD卡內的檔案後才會有這個值

我是這樣改的

然後啟動後的activity
  1. Uri uri = data.getData();
  2. srcPath=uri.toString();
  3. intent.putExtra("srcPath",srcPath);
複製代碼
然後啟動前的activity
  1. getIntent().getStringExtra("srcPath");
複製代碼
還是無法改變textview顯示srcPath的路徑
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
ploglin | 收聽TA | 只看該作者
發表於 2012-7-21 11:46
所以你現在有2個 activity 嗎?

假設有a,b這2個activity
a 傳值給 b

在a就要用 putExtra
在b就要用 getStringExtra
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-7-21 12:16
只有一個activity,是在同一個button後,會跳出選擇檔案的action,然後選完會再跳回去本來的頁面
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則