Take a picture and name it - android

I have built an Android application that allows the user to take pictures, I don't need to save the picture to the photo album but I want to give to picture a custom name and I don't know how to do.
This is how I open the camera:
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
and this is how I save the photo:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
DataManager.bars.get((Integer) getIntent().getSerializableExtra("bar")).images.add(thumbnail); // here I save the picture to my custom object
this.adapter.notifyDataSetChanged(); // I update the recycler view in order to see the pic
} else {
Toast.makeText(this, "Picture Not taken", Toast.LENGTH_LONG).show();
}
}

Related

Android: getting very blurred image from capture camera

I'm capturing image from camera in horizontal scroll-view .When i select that image and display in full image in Image-view its getting very blurry. Why not display clear image in imageview.How to solve this .Thanks in advance
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Log.e("Camera", " Open");
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap photoBitMap = (Bitmap) data.getExtras().get("data");}
}

Detect if Cam is front or back

I take a picture with android :
bComenzarVisita.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_PIC_INI_REQUEST);
}
});
The thing is when the user use front cam, the pic get flipped, and I want to rotate it in this case. Rotating and flipping is no problem.
How can I know in the method : onActivityResult which camera the user has used ( front or back)
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case CAMERA_PIC_INI_REQUEST:
if (resultCode == RESULT_OK) {
// If front camera used, flip pic
}
break;
...
}
}

image capture app crashes on pressing back button

public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 2500;
Button Report_help;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Report_help=(Button)findViewById(R.id.report_help);
Report_help.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) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.display_image);
imageview.setImageBitmap(image);
}
}
}
This app captures the image and displays in the imageview.But the problem is after I capture the image and press the back button app crashes.I don't know why is this so? Please anyone help.
I think when you press back button
Bitmap image = (Bitmap) data.getExtras().get("data");
in onActivityResult cause the Null pointer exception error, please catch this one.
Use the below code to check that case.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}
hope this helps you.

camera activity does not return to the previous/caller activity

I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}

Android.How to use other application data?

how to use data from another application to my application in android?
Is there any way to access data from other application in android?
You should read about sending and receiving content between applications, about content providers and return here with better question.
In this Example Calling a Camera Application using Intent, getting Bitmap and set in ImageView.
startCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("return-data", true);
startActivityForResult(intent);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
Toast.makeText(cameraActivity.this, "Saved", Toast.LENGTH_LONG).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getBaseContext(), "Camera cancelled by user", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources