我想将额外的数据放入 Intent 相机,这看起来很简单......
几天前它还在工作,但是我对代码、avd 和目标版本进行了很多更改,现在它无法正常工作。
该项目现在在目标版本 11 上。
事实上,我的目标是从我的数据库中传递一个 id 来构造图像名称,但这里有一个简单的代码来说明我的问题。
这是示例代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CameraTestActivity" >
<Button
android:id="@+id/buttonpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="Button" />
</RelativeLayout>
-
public class CameraTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_test);
Button buttonpic = (Button)findViewById(R.id.buttonpic);
buttonpic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
///first try to put extra data
cameraIntent.putExtra("thisone", "ArgumentFrom");
///second try to put extra data
Bundle extras = new Bundle();
extras.putBoolean("thisalso",true);
cameraIntent.putExtras(extras);
///start camera activty with ID
startActivityForResult(cameraIntent, 1888);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
///if pic is picked and Intent ID is Camera one i.e. 1888
if (requestCode == 1888 && resultCode == RESULT_OK) {
///first try >> Not working
Bundle extra = getIntent().getExtras();////
if (extra != null) {
Log.d("extra: ","isnotnull");
Boolean inputThatIwant = extra.containsKey("thisone");
Boolean inputThatIwantBool = extra.containsKey("thisalso");
Log.d("thisone",inputThatIwant.toString());
Log.d("thisalso",inputThatIwantBool.toString());
}
///Second try >> Some Data is back (pic data... ?)
Bundle extras = data.getExtras();
if (extras != null) {
Log.d("extras: ","isnotnull"); /////-->>Print "isnotnull"
Boolean inputThatIwant = extras.containsKey("thisone");
Boolean inputThatIwantBool = extras.containsKey("thisalso");
Log.d("thisone",inputThatIwant.toString()); /////-->>Print "false"
Log.d("thisalso",inputThatIwantBool.toString()); /////-->>Print "false"
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_test, menu);
return true;
}
}
[编辑]我最终如何处理这个问题,它也许可以帮助某人......
我只是在同一个班级内“分享”我的 ID:
private String inputid;
当它被相机请求调用时,我在 inputid 字符串中填充了 id,显然在 OnresultActivity 上这个字符串不能为空,所以它的工作方式就这么简单......
但是,如果有人有更好的解决方案,我会接受!
问候....