Android camera picture taking problem?

All your problems with Audio, Video and Images.

Android camera picture taking problem?

Postby mr_muskuraha » Wed Oct 20, 2010 11:32 am

friends,

using following code to use camera to take photo and then display it in imageview to another activity.

now problem i am facing is
this code behaves differently in different firmwares

for example
1) it works fine on HTC hero with 2.1
2) it gives null pointer exception on Galaxy S with 2.1 (while getting bm = (Bitmap)data.getExtras().get("data")
3) it opens camera in nexus one but after taking picture when i press ok button camera is not closed and it does not return to my application :roll:

i am struggling with this problem from last three days and still not got any solution please help me out which is the suitable way to take picture from android camera supported in firmwares 2.1 and 2.2


Code: Select all
string  _path = Environment.getExternalStorageDirectory() + "/images/make_machine_example.png";
Uri selectedImage;

    protected void startCameraActivity()
            {

                File file = new File( _path );
                Uri outputFileUri = Uri.fromFile( file );

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
                intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

                startActivityForResult( intent,0);

            }

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


                switch( resultCode )
                {
                    case 0:
                        Log.i( "MakeMachine", "User cancelled" );
                        break;
                    case -1:
                        bm = (Bitmap)data.getExtras().get("data")
                        onPhotoTaken();
                        break;
                }
            }

protected void onPhotoTaken()
            {


           
                 Bitmap bitmap =bm;
                 

Intent myIntent = new Intent(ActivityA.this, DisplayTakenPhoto.class);
            myIntent.putExtra("ImageBitmap",bitmap);
            startActivity(myIntent);

            }


any help would be appreciated.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Top

Re: Android camera picture taking problem?

Postby tim.choi » Thu Oct 21, 2010 5:42 pm

I am doing similar code and get working well in the emulator, Samsung Galaxy S and SE x10i.

However, I get back my picture from file (ie."_path") not from intent (ie."data"). In fact, your code is also instructing the camera service to save the picture in "_path" not "data", just like mine. So, why don't get it back from proper way?

But remind one thing, you have to guarantee that the file path (not file) must exists if you use above method. Otherwise, you need create it before calling the camera service.
tim.choi
Developer
Developer
 
Posts: 25
Joined: Fri Feb 19, 2010 4:30 pm

Re: Android camera picture taking problem?

Postby mr_muskuraha » Fri Oct 22, 2010 4:29 am

my dear file path cannot be the same all the time.
that is the major reason.

in galaxy S path is like sdcard/DCIM/camera/photoname.jpg

in HTc hero it is sdcard/camera/photoname.jpg
and etc.. different in other mobile phones too.

have you tested your application on Nexus one?

in my case what i understood is how to get path of your camera gallery could only solve the problem.
and that is the reason i am not able to get each phone gallery path dynamically.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Re: Android camera picture taking problem?

Postby tim.choi » Fri Oct 22, 2010 5:15 am

No. no. you pay mistake on it.

You have already defined a particular path for image file output by:

Code: Select all
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );


Then, the camera service won't save the image in its default path only. It will save into this path too. I think you copy the code from somewhere and don't understand the meaning of this... :D

In your program, I can see the path that you defined is: /sdcard/images/make_machine_example.png

"/sdcard" came from Environment.getExternalStorageDirectory(), then you add an additional directory /images and the saved filename is "make_machine_example.png".

"/sdcard" may be different for different mobile phone. However, you don't need to worry. Environment.getExternalStorageDirectory() will do it well and give u a correct path.

Only one thing you need to do is to detect if the path exists or not. If don't exist, create it.
Like:

Code: Select all
String savePath = Environment.getExternalStorageDirectory() + "/images";
File directory = new File(savePath);
if(!directory.exists()) directory.mkdir();
_path=savePath+"/make_machine_example.png";

//then continue your program


After that, you can get it back by above path.

Code: Select all
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;  //if you don't want to reduce the image size, you can omit this or set to 1;
BitmapFactory.decodeFile( _path, options );


That's all, and simple.

I don't have next one and so I cannot check it. But I believe it is workable even running in Next One. The program fail only if the phone don't have SDCARD support. (yes... don't forget to set the SDCARD premission in AndroidManifest file too)
tim.choi
Developer
Developer
 
Posts: 25
Joined: Fri Feb 19, 2010 4:30 pm

Re: Android camera picture taking problem?

Postby mr_muskuraha » Mon Oct 25, 2010 7:32 am

thanks for your detailed help tell me one more thing...


if path does not exists then what should i do?
please explain a bit.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Re: Android camera picture taking problem?

Postby tim.choi » Mon Oct 25, 2010 7:58 am

if not exists, just create it => make the directory. I did it in the above example code like that:

if(!directory.exists()) directory.mkdir();
tim.choi
Developer
Developer
 
Posts: 25
Joined: Fri Feb 19, 2010 4:30 pm

Top

Re: Android camera picture taking problem?

Postby mr_muskuraha » Mon Oct 25, 2010 8:24 am

ah... my fault..
actually never worked on directories thats why..

thanks let me try and get back to you if see something wrong.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Re: Android camera picture taking problem?

Postby mr_muskuraha » Fri Oct 29, 2010 5:26 am

dear i have tried it but it does not create directory in android at given path.
:(

i have used
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in manifest but still it does not create folder there..
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests