I want to show camera's current take image using like given this code.I can get image from camera and display in imageview.I want to know that image's file name.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult(intent, CAMERA_PIC_REQUEST);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 2:
{
if (resultCode == RESULT_OK)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mg_view.setImageBitmap(thumbnail);
}
break;
}
}
}
How can i get image name?please help friends,
Thanks Friends
In your activity (called YourActivity):
public static int TAKE_IMAGE = 111;
Uri mCapturedImageURI;
Somewhere call the camera!
try {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver()
.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, TAKE_IMAGE);
} catch (Exception e) {
Log.e("", "", e);
}
Now in the Activity result (notice capturedImageFilePath)
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if ((requestCode == YourActivity.TAKE_IMAGE)
&& (resultCode == RESULT_OK)) {
mode = MODE_VIEWER;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//THIS IS WHAT YOU WANT!
String capturedImageFilePath = cursor.getString(column_index_data);
bitmap = BitmapFactory.decodeFile(capturedImageFilePath);
}
}
sample code. May be it is useful to you.
Uri mUri;
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "imgnm_"+ String.valueOf(System.currentTimeMillis())+ ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_CAMERA_IMAGE);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CAMERA_IMAGE) {
if (resultCode == RESULT_OK) {
String path = mUri.getPath();
if (path.length() > 0) {
String filepath = path;
String filename = filepath.substring(filepath.lastIndexOf("/") + 1,filepath.length());
String filetype = ".jpg";
Bitmap bm = BitmapFactory.decodeFile(filepath);
mg_view.setImageBitmap(bm);
}
}
}
Try this code,
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
Log.d("ANDRO_CAMERA","Picture taken!!!");
imageView.setImageURI(imageUri);
}
}
}
}
Related
i tried to set on imageview with original quality but the image get compressed.
please help me to set image in imageview with original quality.
ImageView imageView;
private static final int CAMERA_REQUEST = 1888;
Bitmap photo;
uploadImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if(data!=null) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Try using glide, first in your gradle add dependency
compile 'com.github.bumptech.glide:glide.3.7.0'
And use this code to get capture images
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, PICTURE_RESULT);
And then in onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICTURE_RESULT:
if (requestCode == PICTURE_RESULT)
if (resultCode == Activity.RESULT_OK) {
try {
thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imgView.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);
Imageview imageView = (ImageView) findViewById(R.id.image_view);
Glide.with(this).load(imageurl).into(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
And to get the path of image
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
i want to check if photo is taken with front camera with intent method.
below is code for run the camera
public void runCamera(){
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(takePictureIntent, RESULT_CAMERA);
}
then below is activity result to process the taken image.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
...
case RESULT_CAMERA:
if(resultCode == RESULT_OK && mCapturedImageURI!=null){
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);
String filePath;
if (cursor != null) {
cursor.moveToFirst();
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
filePath = cursor.getString(column_index_data);
}
else
filePath = data.getData().getPath();
GeneralizeImage mGI = new GeneralizeImage(this,filePath);
//getOrientationImage();
uploadFileToServer(mGI.Convert());
cursor.close();
}
else{
Toast.makeText(this, "Try Again", Toast.LENGTH_LONG).show();
}
break;
i appreciate any help from you guys, thanks.
You can pass parameter in your intent to open front camera.
Try this
takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
In my Activity A, there is an ImageView and a Button. When the button is clicked, it goes to activeTakePhoto(). The imgUri gets displayed but nothing is displayed in my ImageView.
private void activeTakePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE &&
resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE &&
resultCode == RESULT_OK) {
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
ImageView imgView=(ImageView)findViewById(R.id.imageView);
Uri imgUri=Uri.fromFile(picture);
imgView.setImageURI(imgUri);
Toast.makeText(getApplication(),imgUri+"",Toast.LENGTH_LONG).show();
}
}
}
You can try this code,it may help:
#Override
public void onClick(View v) {
if (v == imgCamera) {
Toast.makeText(getApplicationContext(), "open camera", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
}//on click
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("RESULT CODE", "--" + resultCode);
if (resultCode == RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//captured image set in imageview
imageView.setImageBitmap(photo);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
in activeTakePhoto
Replace
String fileName = "temp.jpg";
with
fileName=Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.jpg"
I call camera intent and try to save capture image in sd card.but camera intent give me null data.what is the problem i can't understood.my code is as follow
imgBtnPicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String fileName="ashraful.jpg";
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/photofolder").mkdirs();
File outputfile=new File(root+"/photofolder/", fileName);
Uri outputFileUri = Uri.fromFile(outputfile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if(data!=null)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
Bitmap icon1=ResizeBitmap(photo,200,200);
imgBtnPicture.setImageBitmap(icon1);
}
}
}
but i get null data? how to solve my problem?plz help
private void takeNewPicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
cameraImagePath = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Uri imageFilePathUri = null;
if (cameraImagePath != null) {
Cursor imageCursor = getContentResolver().query(
cameraImagePath, filePathColumn, null, null, null);
if (imageCursor != null && imageCursor.moveToFirst()) {
int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]);
String filePath = imageCursor.getString(columnIndex);
imageCursor.close();
imageFilePathUri = filePath != null ? Uri
.parse(filePath) : null;
}
}
}
}
cameraImagePath is a Uri object
Read this Android Training post carefully to figure things out. As the post says,
The Android Camera application saves a full-size photo if you give it a file to save into.
so if you did pass the Uri of a file, you should access the image using this Uri.
I'm trying to get the file path of pictures that I took with Camera Intent as a String, but the String filePath is always null. What am I doing wrong?
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(openCamera, OPEN_CAMERA);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case OPEN_CAMERA:
if (resultCode == RESULT_OK && data != null) {
Uri captureImage = data.getData();
String filePath = captureImage.getPath();
break;
}
}
}
This is how I get the image that was taken with the camera.
I create the file before and when the image gets saved then it gets saved to my file..
File externalFile = new File("Whatever you want the path to be...");
Uri uriSavedImage=Uri.fromFile(externalFile);
Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);
Then when the result is received.
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap photo = BitmapUtils.decodeFileForDisplay(new File("Whatever your file's path is");
}
}
}
try the following passing your captureImage Uri as parameter:
public String getRealPathFromURI(Uri contentUri) {
String[] projx = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, projx, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
EDIT:
that is a common bug that getData() returns null on some devices. You need to use a pre-inserted Uri to prevent that. Example:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
preinsertedUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Getting the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode != 0 && data != null) {
Uri imageUri = preinsertedUri;
}
break;
}