2014-09-19 81 views
0

我有這樣的方法來掃描拍攝鏡頭與我的應用程序後,文件的媒體:獲取參數

public static void refreshMedia() { 
     MediaScannerConnection.scanFile(this, 
       new String[] { photo.toString() }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
      public void onScanCompleted(String path, Uri uri) { 
       Log.i("ExternalStorage", "Scanned " + path + ":"); 
       Log.i("ExternalStorage", "-> uri=" + uri);    
      } 
     }); 
    } 

現在我需要以檢索,它是此方法的參數的URI ..我怎麼能做它?因爲我需要使用URI在另一個類..

回答

1
private static Uri mUri; // private variable in your class 
.... 
public static void refreshMedia() { 
    MediaScannerConnection.scanFile(this, 
      new String[] { photo.toString() }, null, 
      new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
      mUri = uri; //<--Add this line to assign uri to mUri 
      Log.i("ExternalStorage", "Scanned " + path + ":"); 
      Log.i("ExternalStorage", "-> uri=" + uri);    
     } 
    }); 
} 

編輯:

之前開始新的意圖:在其他類

Intent i = new Intent(FirstScreen.this, SecondScreen.class); 
i.putExtra("MY_Uri", mUri); 

String mUri; //private variable 

onCreate()

Bundle extras = getIntent().getExtras(); 
if(extras == null) { 
    mUri= null; 
} else { 
    mUri= extras.getString("MY_Uri"); 
} 
+0

不,變量必須是靜態的。否則'mUri'將重置爲默認值,如果類實例化 – 2014-09-19 10:39:48

+0

好的。你已經編輯過。 – 2014-09-19 10:40:13

+1

是的,我編輯了我的代碼.. :) – 2014-09-19 10:40:14

1

用getter和setter創建一個static類變量。

private static Uri uri; 

然後存儲uri從其他地方得到這個值。

public static void refreshMedia() { 
    MediaScannerConnection.scanFile(this, 
      new String[] { photo.toString() }, null, 
      new MediaScannerConnection.OnScanCompletedListener() { 
       public void onScanCompleted(String path, Uri uri) { 
        YourClassName.uri=uri; // store uri 
        Log.i("ExternalStorage", "Scanned " + path + ":"); 
        Log.i("ExternalStorage", "-> uri=" + uri); 
       } 
      }); 
}