我想请教一些有关使用相机应用程序的帮助。首先,我阅读了所有Android Developer API 指南
在我尝试创建我的第一个使用相机的应用程序之后。我复制了所有源代码,但不幸的是我遇到了一个问题:当用户使用相机完成时,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>
主.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" />
主要活动:
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 的数据参数为空。
有人可以帮忙解决问题吗?
非常感谢,再次抱歉这个问题!!!