Android open camera from button
To call the camera you can use:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
The image will be automatically saved in a default directory.
And you need to set the permission for the camera in your AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA"> </uses-permission>
ANDROID -How to open camera when a button is pressed in android?
Try this
YourButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
The image will be automatically saved in a default directory.
And you need to set the permission for the camera in your manifest.xml
:
<uses-permission android:name="android.permission.CAMERA"> <uses-permission>
How to open camera app in android
You can open camera app with intent
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Now get the image back once you are done with camera app
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
// do whatever you want with the image now
}
}
Hope this would help.
Open camera directly in the Activity without clicking on button and without intent?
I found my error. I called mCamera.setPreview()
in the onSurfaceCreated()
method of my CameraPreview class.
I need to call mCamera.setPreview()
in the onSurfaceChanged()
! Now it works
Problem solved !
How to open camera , or call startActivityForResult from a button in recycleriew item
Try this code :
Call following method inside your if(view.getId() == act_photo.getId()){ HERE }
public void OpenCamera(){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
view.getContext().startActivity(intent);
}
don't forget to add permission to your AndroidManifest.xml file :
<uses-permission android:name="android.permission.CAMERA"/>
Additionally for Marshmallow onwards you need to handle Runtime permission.
I hope this will help.
ImageView with camera button at bottom
Well You can use this layout..
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:ignore="MissingPrefix">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/bicycle" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/margin_5dp"
android:clickable="true"
app:fabSize="mini"
android:src="@android:drawable/ic_menu_camera"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
Hope it'll help
Capture Image from Camera and Display in Activity
Here's an example activity that will launch the camera app and then retrieve the image and display it.
package edu.gvsu.cis.masl.camerademo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MyCameraActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private static final int MY_CAMERA_PERMISSION_CODE = 100;
@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)
{
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.
Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
And one final detail, be sure to add:
<uses-feature android:name="android.hardware.camera"></uses-feature>
and if camera is optional to your app functionality. make sure to set require to false in the permission. like this
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
to your manifest.xml.
Related Topics
How to Compile C into an Executable Binary File and Run It in Android from Android Shell
Prevent Ussd Dialog and Read Ussd Response
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Android Studio Doesn't Recognize My Device
How to Display Long Messages in Logcat
Android How to Create Triangle and Rectangle Shape Programmatically
Android: Making a Fullscreen Application
Dialogfragment with Clear Background (Not Dimmed)
Error:Execution Failed for Task ':App:Transformclasseswithdexfordebug' in Android Studio
Show Error on the Tip of the Edit Text Android
How to Retrieve a List Object from the Firebase in Android
Highlight Custom Listview Item When Long Click
How to Measure Textview Height Based on Device Width and Font Size