I would like ask some help about using camera app. First I read everything Android Developer API guides
After I tried to create my first app whicj is using camera. I copy all source code but unfortunatly I conflict a problem: onActivityResult data params is NULL when user finish with camera using :(
I read as much as I can questions and answerws here but I can not find my soultion :( My code is the next:
Manifest
<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();
}
}}
I tried that I set extra params like:
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);
}
});
...but all time I get that data params of onActivityresult is null.
Could anybody help what the problem is?
Thank you so much and sorry again this question!!!