我想问一些关于使用相机应用程序的帮助。 首先我读了所有的东西Android Developer API guidesAndroid onActivityResult数据parasm使用相机返回null
当我试图创建我的第一个应用程序正在使用相机。我复制所有的源代码,但不幸我冲突的一个问题:onActivityResult数据参数为NULL时,用户完成相机使用:(
我尽可能多的阅读,但我找不到我的灵魂:( 我的代码是下一个:
清单
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="hu.abanhidy.android.cameramanager.CameraManagerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
的main.xml:
<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"
tools:context=".CameraManagerActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Make Photo" />
MainActivity:
public class CameraManagerActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button takePhoto = (Button)findViewById(R.id.btnTakePhoto);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "result code: " + resultCode, Toast.LENGTH_LONG).show();
if (data != null){
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(this, "User cancelled the image capture", Toast.LENGTH_LONG).show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image capture failed, advise user", Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(this, "Data is null!", Toast.LENGTH_LONG).show();
}
}}
我想,我设置额外参数,如:
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
fileUri = Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
......但所有的时间我得到onActivityresult的数据params为null 。
有人能帮助解决问题吗?
非常感谢你,再次对不起这个问题!
嗯......有可能你是对的。我已经检查过了。如果我也知道它RESULT_CANCELED = 0; RESULT_OK = -1。只有这样,我可以返回我的应用程序,按两次后退“按钮”。我再次查询并尽快回复。 Thx – aBanhidy
恩......你说的对。我发现一个按钮,我的手机相机应用程序,只需点击一下就可以返回到我的应用程序。我接受了-1 = RESULT_OK代码,并且数据不为空。谢谢......但我担心它不会对我有好处......但这是另一回事:) – aBanhidy