I have a problem while getting an image taken by the camera in my app.
When I try to get the file, it doesn't exists. I don't see where the problem is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.parse("/sdcard/Images/test_image.jpg");
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
File image=new File("/sdcard/Images/","test_image.jpg");
if (image.exists()){
Bitmap bm = BitmapFactory.decodeFile("/sdcard/Images/test_image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
}
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
When it reach to image.exists() it says false. I already wrote sd write permissions on manifest.
Uri.parse Creates a uri which parses the given encoded URI string instead of file path with is not exist on sdcard. so either user "file://"+ filename_with_path or use Uri.fromFile to create URI from file path. also use Environment.getExternalStorageDirectory() instead of static path for sdcard as:
File dir = Environment.getExternalStorageDirectory();
file = new File(dir, "test_image.jpg");
Uri uri = Uri.fromFile(file);
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(photoIntent, CAMERA_REQUEST);
Second major issue is you are passing cameraIntent intent in which you are not adding MediaStore.EXTRA_OUTPUT to startActivityForResult. so pass photoIntent to startActivityForResult because you are adding MediaStore.EXTRA_OUTPUT key in photoIntent Intent instance instead of cameraIntent .
You can check your path and if your folder exists with this :
File[] file = Environment.getExternalStorageDirectory().listFiles();
It gives you the sdcard path and all its folders.
Related
In my app the user can take an image from the camera and use as a profile picture. Everything works, except that when the user leaves the app and returns then the image isn't there anymore. So how can I save that image to stay there even when the user exits the app?
Code for camera intent:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
And onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
getLayoutInflater().inflate(R.layout.custon_header,null);
ImageView profimg = (ImageView) findViewById(R.id.roundedimg);
profimg.setImageBitmap(photo);
Could someone please assist me with this?
Thanks
data.getExtras().get("data");
only returns the thumbnail of the image which would be on low quality. You have to specify on your camera intent a location where the image will be saved:
File outputFile = new File(Environment.getExternalStorageDirectory() +"/myOutput.jpg");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(Intent.EXTRA_OUTPUT,outputFile.getAbsolutePath());
startActivityForResult(cameraIntent, CAMERA_REQUEST);
and on your activityResult, simply use the outputfile for your bitmap:
bitmap = BitmapFactory.decodeFile(outputFile.getAbsolutePath());
You can configure the intent to store the image, using EXTRA_OUTPUT like this:
Intent imageIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File imageFolder = new File(Environment
.getExternalStorageDirectory(), "YourFolderName");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
}
File imageFile = new File(imagesFolder, "user.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
imageIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
activity.startActivityForResult(imageIntent, CAMERA_REQUEST);
So you have the path and can decode the file to bitmap.
You need to save that image in sd-card or phone memory so that it can be accessed when you open your app again. You are seeing that image as soon as you take the image because it is there in the memory and as soon as you leave the app, the memory is released.
private void SaveBitmapInDirectory(Bitmap bitmap, String fileName)
{
File direct = new File(Environment.getExternalStorageDirectory() + "/dirName");
if (!direct.exists())
{
File profilePic = new File("/sdcard/dirName/");
profilePic.mkdirs();
}
File file = new File(new File("/sdcard/dirName/"), fileName);
if (file.exists())
{
file.delete();
}
try
{
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
And load the file from directory in onCreate function by calling the following function:
void loadProfilerImage(String fileName)
{
File file = new File(new File("/sdcard/dirName/"), fileName);
if (file.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView.setImageBitmap(myBitmap);
}
}
I just want to save a picture in my Imagefolder in my phone.
I have got 2 examples which I tried.
1. Example
My app crashes when I activate the onClick Method:
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);
}});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == 1337)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}
else
{
Toast.makeText(AndroidCamera.this, "Picture NOt taken", Toast.LENGTH_LONG);
}
super.onActivityResult(requestCode, resultCode, data);
}
2. Example
Before I saved my taken Picture with Uri. But it saved my picture in a folder, which I can only access on my PC or with a FileApp. I donĀ“t know how I can change the Path direction with Uri to my existing default image folder in my phone.
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
This is how I manage with saving images to specified imagefolder
When starting camera intent I define path and directory, where my image should be saved, and pass this as intetn extra when starting camera:
private void startCameraIntent() {
//create file path
final String photoStorePath = getProductPhotoDirectory().getAbsolutePath();
//create file uri
final Uri fileUri = getPhotoFileUri(photoStorePath);
//create camera intent
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//put file ure to intetn - this will tell camera where to save file with image
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start activity
startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO_FROM_CAMERA);
//start image scanne to add photo to gallery
addProductPhotoToGallery(fileUri);
}
And here are some of helper methods used in code above
private File getProductPhotoDirectory() {
//get directory where file should be stored
return new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),
"myPhotoDir");
}
private Uri getPhotoFileUri(final String photoStorePath) {
//timestamp used in file name
final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.US).format(new Date());
// file uri with timestamp
final Uri fileUri = Uri.fromFile(new java.io.File(photoStorePath
+ java.io.File.separator + "IMG_" + timestamp + ".jpg"));
return fileUri;
}
private void addProductPhotoToGallery(Uri photoUri) {
//create media scanner intetnt
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//set uri to scan
mediaScanIntent.setData(photoUri);
//start media scanner to discover new photo and display it in gallery
this.sendBroadcast(mediaScanIntent);
}
I am trying to capture an image using Android Camera Intent. Camera intent returns Byte Array and when I saved the byte array as a Bitmap, I am getting a very small image instead of getting an Image based on current camera settings (1024 Pixels currently set in the android mobile camera).
Usally i will get file path from the camera intent but somehow i am not getting from this device, so i am creating the bitmap from the byte returned by camera intent.
Anybody knows why is this and how to solve this issue. Thanks.
The below is the java code block I am using.
private Intent cameraIntent = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
if ( data != null)
{
Bitmap myImage = null;
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
byte[] byteArray = stream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
myImage = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length, options);
fileOutputStream = new FileOutputStream(sPath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}
}
}
}
The data.getExtras("data") only returns a thumbnail image. To obtain the full-sized image, you need to pass the camera intent a file in which to store that image and later retrieve. A rough example follows.
Start the intent:
File dir = new File(Environment.getExternalStorageDirectory() + "/dcim/myappname");
File mFile = File.createTempFile("myImage", ".png", dir);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Inside onActivityResult:
if (resultCode == RESULT_OK) {
Bitmap bm = BitmapFactory.decodeFile(mFile.getAbsolutePath());
}
// do whatever you need with the Bitmap
Keep in mind that mFile will have to be global or somehow persisted so that it may be called upon where necessary.
I am using the following code to take picture, using the device camera. I am new to android. Can anybody please help me and tell me where I should specify the path. I want to save images in a separate folder in sd card. Any help is deeply appreciated.
private static final int CAMERA_PIC_REQUEST = 2500;
bcontinue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_PIC_REQUEST && resultCode==RESULT_OK)
{
try{
Byte image1 = (Byte) data.getExtras().get("data");
FileOutputStream fos = openFileOutput("filename.bmp", Context.MODE_PRIVATE);
fos.write(image1);
fos.close();
}
catch(Exception e){
}
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(image);
Context context = getApplicationContext();
CharSequence text = "Click on the image!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
The below code will start the default camera and have the camera save the image to the specified uri. The key is to put the extra "MediaStore.EXTRA_OUTPUT" along with the desired uri.
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg");
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);
I am new to this site and android. how to save the camera's picture into specific folder and save the picture's name into sqlite databse also.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.camera);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
onActivityResult(1337, 0, cameraIntent);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == CAMERA_PIC_REQUEST) {
}
I opened camera activity. How to get the picture name & save it to particular location?
please help me on this.
thank you in advance.
I'm calling camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
and saving to sd card like this
final ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(100, 100));
image2 = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image2);
String incident_ID = IncidentFormActivity.incident_id;
//l2.addView(img);
imagepath="/sdcard/RDMS/"+incident_ID+ x + ".png";
File file = new File(imagepath);
try {
bm = Bitmap.createScaledBitmap( image2,400, 300, true);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(CompressFormat.PNG, 90, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"yourfirst error message is "
+ e.toString(), 1000).show();
}
You may need to change the line
if (resultCode == CAMERA_PIC_REQUEST) {
to
if (requestCode == CAMERA_PIC_REQUEST) {
You are already saving the picture. You provided the location in MediaStore.EXTRA_OUTPUT.
Link
Doing a DB you will have to have a look at this link is a little bit to much to explain