Captured image does not display on ImageView - android

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"

Related

Click Image By Phone Camera and set to ImageView in Original Quality

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);
}

android read file from sd card

I just need to covert the path in file to InputStream here is my code. I select the path by calling Intent but it always gives me filenotfoundexception
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK) {
File file = new File(Environment.getExternalStorageDirectory(), data.getData().toString());
Uri path = Uri.fromFile(file);
File fpath=new File(path.toString());
try {
InputStream myfile=new FileInputStream(fpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
thanks for help in advance...
try this:
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
InputStream myfile=cr.openInputStream(uri));;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setType("image/*");
//intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity(). RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
//Setting the Bitmap to ImageView
Glide.with(getActivity()).load(filePath).asBitmap().placeholder(R.drawable.ic_launcher).into(avatarContainer);
//avatarContainer.setImageBitmap(bitmap);
//uploadFile(filePath.getPath());
try {
uploadFile(getPath(filePath));
} catch (Exception e) {
Config.displayToast("Please Select Image From Gallary");
alertDialog.dismiss();
ServerRequest.dismissDialogBox(context);
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}else if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == getActivity() .RESULT_OK){
String filepath = fileUri.getPath();
Log.d("Filepath is",""+filepath);
BitmapFactory.Options options = new BitmapFactory.Options();
// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
//avatarContainer.setImageBitmap(bitmap);
Glide.with(getActivity()).load(filepath).asBitmap().placeholder(R.drawable.ic_launcher).into(avatarContainer);
uploadFile(filepath);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Android camera capture activity returns null Uri

This code worked on samsung before but now that i'm using Nexus One with Android 2.3.6, it's crashing as soon as I take a picture and click ok or choose a photo from gallery. Stacktrace shows a null pointer exception on the Uri.
My code for the activating the camera is as follows:
public void activateCamera(View view){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == CHOOSE_IMAGE_ACTIVITY_REQUEST_CODE || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
&& resultCode == RESULT_OK && null != data) {
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();
Bitmap bits = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
try {
bits = BitmapFactory.decodeStream(new FileInputStream(picturePath),null,options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any idea what could be the problem?
Thanks!
You have to tell the camera, where to save the picture and remeber the uri yourself:
private Uri mMakePhotoUri;
private File createImageFile() {
// return a File object for your image.
}
private void makePhoto() {
try {
File f = createImageFile();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, REQUEST_MAKE_PHOTO);
} catch (IOException e) {
Log.e(TAG, "IO error", e);
Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
}
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}
You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().
Inject this extra into the Intent that called onActivityResult and the system will do all the heavy lifting for you.
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
Doing this makes it as easy as this to retrieve the photo as a bitmap.
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(mImageBitmap);
}
dont pass any extras, just define the path where you have placed or saved the file directly in onActivityResult
public void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = createImageFile();
boolean isDirectoryCreated = file.getParentFile().mkdirs();
Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);
if (Build.VERSION.SDK_INT >= 23) {
tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
"com.scanlibrary.provider", // As defined in Manifest
file);
} else {
tempFileUri = Uri.fromFile(file);
}
try
{
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
}
catch (Exception e)
{
}
}
private File createImageFile() {
clearTempImages();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
Date());
File file = new File(ScanConstants.IMAGE_PATH, "IMG_" + timeStamp +
".jpg");
fileUri = Uri.fromFile(file);
return file;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null ;
if (resultCode == Activity.RESULT_OK ) {
try {
if (Build.VERSION.SDK_INT >= 23) {
tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
"com.scanlibrary.provider", // As defined in Manifest
file);
} else {
tempFileUri = Uri.fromFile(file);
}
bitmap = getBitmap(tempFileUri);
bitmap = getBitmap(data.getData());
} catch (Exception e) {
e.printStackTrace();
}
} else {
getActivity().finish();
}
if (bitmap != null) {
postImagePick(bitmap);
}
}

Save Capture Image in SD card android

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.

how to get image name using camera intent in android?

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);
}
}
}
}

Categories

Resources