2013-02-19 181 views
0

问题:活动1有图像路径,当活动1通过意图发送图像文件时,活动2具有到不同图像的不同路径。图像路径不覆盖

代码活动1:

... 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     imageFile = new File(outputFileUri.getPath()); 
    } else if (requestCode == UPLOAD_REQUEST && resultCode == RESULT_OK) { 
     //Get the data back from the request in URI format (Path to file). 
     selectedImage = data.getData(); 
     imageFile = new File(getRealPathFromURI(selectedImage)); 
    } else if (resultCode == RESULT_CANCELED) { 
     return; 
    } 
    Intent menuIntent = new Intent(getApplicationContext(), MenuActivity.class); 
    menuIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
    menuIntent.putExtra("username", username); 
    menuIntent.putExtra("latitude", latitude); 
    menuIntent.putExtra("longitude", longitude); 
    menuIntent.putExtra("imageFile", imageFile); 
    Log.d("ACTIVITY1 image", imageFile.getPath());//THIS LOG BELOW!! 
    try{ 
     startActivity(menuIntent); 
     finish(); 
    } 
    catch(ActivityNotFoundException e){ 
     e.printStackTrace(); 
     menuIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 
     startActivity(menuIntent); 
     finish(); 
    } 
} 

原因在意图尝试捕捉是被用于从该一个不同的使用开此酶活性。 代码活动2:

... 
@Override 
public void onStart() { 
    super.onStart(); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     username = extras.getString("username"); 
     latitude = extras.getDouble("latitude"); 
     longitude = extras.getDouble("longitude"); 
     if(extras.get("imageFile") != null){ 
      File imageFile = (File) extras.get("imageFile"); 
      Log.d("ACTIVITY2 image", imageFile.getPath());//THIS LOG BELOW!! 
      toastContext = getApplicationContext(); 
      params = new ArrayList(); 
      params.add("upload"); 
      params.add(username); 
      params.add(imageFile); 
      uploadImage = new UserFunctions(this); 
      uploadImage.execute(params); 
     } 
    } 
} 

有经由POST将文件发送到服务器并返回JSON,以形成根据所述响应它的通知和更新的的AsyncTask。那里没有问题。 如果用户登录,则加载活动2,如果不是,则在用户登录时稍后调用活动2。 当活动2通过buttonListener加载活动1,然后活动1要么拍照时,选择一个文件。当活动1已经决定时,它立即再次加载活动2,在意图中传递新的imageFile。当活动2加载时,它将请求发送给服务器。返回 日志是: 第一次的图像通过POST发送:

DEBUG/ACTIVITY1 image(15559): /storage/emulated/0/DCIM/Camera/IMG_20130215_193022.jpg 

然后传递通过意向活动2 ...

DEBUG/ACTIVITY2 image(15559): /storage/emulated/0/DCIM/Camera/IMG_20130215_193022.jpg 

第二次,选择不同的图像或服用后新的照片:

DEBUG/ACTIVITY1 image(15559): /storage/emulated/0/DCIM/Camera/IMG_20130214_212205.jpg 

然后传递通过意向活动2 ...

DEBUG/ACTIVITY2 image(15559): /storage/emulated/0/DCIM/Camera/IMG_20130215_193022.jpg 

如果有ASyncTask/onPostExecute请求,我会发布它!

谢谢!

编辑:新增Android清单澄清活动:

<activity android:name=".ACTIVITY1" android:label="@string/app_name" 
       android:screenOrientation="portrait"/> 
    <activity android:name=".ACTIVITY2" 
       android:label="@string/app_name" 
       android:parentActivityName=".ACTIVITY1" 
       android:excludeFromRecents="true" 
       android:hardwareAccelerated="true" 
       android:launchMode="singleTop" 
       android:taskAffinity="" 
       android:screenOrientation="portrait"> 
    <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".ACTIVITY1"/> 
    </activity> 

回答

0

哦男人,我觉得不好...

活动2之后开始的AsyncTask:

uploadImage = new UserFunctions(this); 
uploadImage.execute(params); 

活动需求结束。 添加

finish(); 

修复它。