如何使用startActivityForResult的活動範圍之外的一個普通的Java類startActivityForResult某項活動
EX說。在這種方法中,我必須打開設備相機並獲取圖像並對其進行處理。
問題是控件不會去往SampleClass裏面的onActivityResult回調方法,而是它將在MainActivity內部的onActivityResult回調方法。我究竟做錯了什麼?
我只需要能夠處理Sample Class內的圖像。我怎樣才能做到這一點
代碼段
主要活動:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
SampleClass sample = new Sample(this);
sample.openCamera();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
syso("INSIDE MAIN ACTIVITY");
}
if(resultCode == RESULT_CANCELED){
...
}
}
}
}
示例類:
public class SampleClass extends Activity{
private Context context;
public SampleClass(Context context){
this.context = context;
}
public void openCamera(){
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) context).startActivityForResult(photoIntent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
}
如果我運行這個程序,控制檯上的輸出是INSIDE MAIN ACTIVITY。我想要的是能夠將INSIDE SAMPLE CLASS作爲輸出。
感謝和問候
向我們展示openCamera(); – NoXSaeeD 2014-10-09 13:50:02