tags:

views:

103

answers:

2

In my application in android 1.6 I m calling the default camera using

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

and mentioning output directory

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/image.jpg")));

This works fine. However, if I replace it by

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getDataDirectory() + "/image.jpg")));

the ok (attach) button doesnt respond. I have added the following in AndroidManifest.xml

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

How to solve this ?

A: 
Environment.getDataDirectory().getAbsolutePath()+"/data/your package"+"/image.jpg"

You have to save the file inside /data/your package/file name

Rahul
I wouldn't do it that way. That's what Context.getFilesDir() is for.
EboMike
+1  A: 

Environment.getDataDirectory() should not be of interest to you. This is Android's private data directory, and there's really no reason for any app to access it.

If anything, use Context.getFilesDir(). But keep in mind that even that is by default private to your app, so other applications (including the media store) will not be able to access it. That said, you always have the option to make files world read or writeable.

EDIT: You can use Context.getDir() with Context.MODE_WORLD_WRITEABLE to write a directory that other apps can write into. But again, I question the need to store image data in local storage. Users won't appreciate that, unless users are expected to not have their SD card mounted while using your app (which is not common).

EboMike
Thanks Tried this.. But not working. I m calling mediastore. So any workaround ?
Prabhat
"Tried this" meaning that you tried getFilesDir(), or that you made the files world-readable? Also, is there any reason why you don't put them on the SD card? With internal storage being limited, I don't see a reason to burden it with image data.
EboMike
Yes. I tried getFilesDir(). It did not work. The point is if user removes SD card and runs the application, it'l crash. So I want to store it in data memory of phone.
Prabhat
i think it will be an edge case scenario where in a user runs the application without a SD Card. You can check whether the device has an SDCard inserted or not when you launch your application.
Rahul
Like I said, you will need to make the files world-readable as well! Look up Context.MODE_WORLD_READABLE, which you pass into Context.openFileOutput.
EboMike
The image itself is not being written. It goes to camera UI then on click of ok (attach) button there is no response, the camera UI still remains. Only way out of it is by clicking cancel
Prabhat
Other apps are not allowed to write into your data directory. You could create a world-writeable directory within your data directory using Context.getDir(), passing in Context.MODE_WORLD_WRITEABLE.
EboMike
I tried intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(Uri.fromFile(context.getDir("images", Context.MODE_WORLD_WRITEABLE))+ "/img.jpg")); as you said. It works, however the image stored is a thumbnail image 54x88 in android 1.6. It works fine in android 2.1
Prabhat