Brett
Brett

Reputation: 441

Android camera application

I m new in android.. and I want to create a camera android application..

These are the following codes... It works fine when i m starting my app in my phone [ galaxy note n7000] but after capturing an image a problem appears.. when i click save button an error dialouge appeares and my app stopped..I think there is some problem in onActivityResult() method .. please help..

Activity_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"
    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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp"
        android:text="Take photo" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="49dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java

package com.example.cameratest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn= (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,0);
            }  });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bm= (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(bm);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cameratest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.cameratest.MainActivity"
            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>

</manifest>

Upvotes: 0

Views: 1103

Answers (3)

Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

exact source of error could be found once you show your logcat output but it seems like the problem is that you haven't initialized ImageView iv anywhere but your are setting bitmap for it in the onActivityResult method. First initialize the imageView somewher; may be in the onCreate method. e.g do this in your onCreate method :

    ImageView iv = (ImageView)findViewById(R.id.some_id);

Upvotes: 0

sandy
sandy

Reputation: 3341

Try This

Start Camera like this, use this code in onCreate method.

        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        String filePath = Environment.getExternalStorageDirectory() + "";

        filePath = filePath + File.separator + "temp_img.jpg";
        File f = new File(filePath);
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Uri uri = Uri.fromFile(f);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, 0);

onActivityResult method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // super.onActivityResult(requestCode, resultCode, data);
    Uri currImageURI = null;
    if (resultCode == Activity.RESULT_OK) {
        if (data != null) {
            currImageURI = data.getData();
        }
        if (currImageURI == null) {
            String filePath = Environment.getExternalStorageDirectory()
                    + "";
            filePath = filePath + File.separator + "temp_img.jpg";
            File f = new File(filePath);

            currImageURI = Uri.fromFile(f);
        }

        ContentResolver cr = getContentResolver();
        Bitmap bitmap;
        try {
            bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr,
                    currImageURI);
            iv.setImageBitmap(bitmap);
        } catch (Exception e) {
            Log.e("Camera", e.toString());
        }
    }
}

And Make sure to initialize iv in onCreate.

iv=(ImageView) findViewById(R.id.imageView1);

In Last add permission to write file on sdcard.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

Vishal Mokal
Vishal Mokal

Reputation: 800

Hi Welcome to Android I also have done this assignment when i have started. here is the code for camera.java

`package com.example.android;

import java.io.IOException; import java.io.InputStream;

import android.app.Activity; import android.app.WallpaperManager; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView;

public class Camera extends Activity implements OnClickListener {

ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bm;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo);
    initialize();
    InputStream is = getResources().openRawResource(
            R.drawable.splash_background);
    bm = BitmapFactory.decodeStream(is);
}

public void initialize() {
    ib = (ImageButton) findViewById(R.id.ibTakePic);
    b = (Button) findViewById(R.id.bSetWall);
    iv = (ImageView) findViewById(R.id.ivReturnedPic);

    b.setOnClickListener(this);
    ib.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bSetWall:
        try {
            WallpaperManager.getInstance(getApplicationContext())
                    .setBitmap(bm);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;
    case R.id.ibTakePic:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, cameraData);
        break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bm = (Bitmap) extras.get("data");
        iv.setImageBitmap(bm);
    }
}

}

Here is xml for activity

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/bSetWall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Set Wall" />

<ImageView
    android:id="@+id/ivReturnedPic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/ibTakePic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/splash_background" />

check this linkVideo tutorial for android

Upvotes: 1

Related Questions