问题:当 Activity 1 通过 Intent 发送图像文件时,Activity 1 具有指向图像的路径,而 Activity 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();
}
}
尝试捕获 Intent 的原因是此活动被用于除此之外的其他用途。代码活动 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);
}
}
}
有一个 ASyncTask 通过 POST 将文件发送到服务器,并返回 JSON 以形成通知并根据响应进行更新。那里没有问题。如果用户登录,则加载活动 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 Manifest 以澄清活动:
<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>