4

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!!!

aBanhidy
  • 257
  • 1
  • 3
  • 11

2 Answers2

2

Check that you get Activity.RESULT_OK as the resultcode. If not you've either cancelled the take picture operation or something else went wrong. I.e., add this:

Toast.makeText(this, "result code was ok: " + (resultCode == Activity.RESULT_OK), Toast.LENGTH_LONG).show();
aeliusd
  • 469
  • 7
  • 18
  • Hmm...it is possible that you are right. I already checked it. If I know it as well RESULT_CANCELED = 0; RESULT_OK = -1. Only way how I can return my app that press twice the back "button". I chaeck it again and answer soon. Thx – aBanhidy Jan 15 '13 at 14:54
  • Well...you were right. I found a button my phone camera app which go back to my app on only one click. I receaved an -1 = RESULT_OK code, and data was not null. Thank you...but I am affraid it wont be good for me...but this is another story :) – aBanhidy Jan 15 '13 at 14:58
2

Ok! I think I understand this process. If you set extraOutput Uri the onActivityResult data params is null...because you don't need example Uri information where the image is! You already set it. If you don't set this information you have to know where the image is.

So...I understand this process but I really don't agree with it!!!

aBanhidy
  • 257
  • 1
  • 3
  • 11