Launch Android Camera Intent And Layout
I am trying to load the camera to take a photo from my android app, my Photos.java is private Uri imageUri; public void takePhoto(View view) { Intent intent = new Intent('andr
Solution 1:
Give this a try...
NOTE : Only applicable to Android API 8 // 2.2 or higher
publicclassPhotoActivityextendsActivity {
/** The Constant PICK_IMAGE. */privatestaticfinalintPICK_IMAGE=0;
/** The Constant PICK_IMAGE_FROM_GALLERY. */privatestaticfinalintPICK_IMAGE_FROM_GALLERY=1;
/** The btn cancel. */private Button btnPhotoCamera,btnPhotoGallery,btnCancel;
/** The img view. */private ImageView imgView;
/** The u. */private Uri u;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_options);
imgView=(ImageView)findViewById(R.id.imgDisplayImage);
btnPhotoCamera=(Button)findViewById(R.id.btnPhotoCamera);
btnPhotoGallery=(Button)findViewById(R.id.btnPhotoGallery);
btnCancel=(Button)findViewById(R.id.btnCancel);
btnPhotoCamera.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
Intent camera=newIntent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "true");
File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
u = Uri.fromFile(newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(camera, PICK_IMAGE);
}
});
btnPhotoGallery.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
Intentintent=newIntent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_FROM_GALLERY);
}
});
btnCancel.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
Intent goStartUp=newIntent(PhotoActivity.this, StartUpActivity.class);
goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goStartUp);
finish();
}
});
}
/* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stubif (resultCode==RESULT_OK )
{
if(requestCode == PICK_IMAGE) {
InputStream is=null;
try {
is = this.getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp=BitmapFactory.decodeStream(is);
imgView.setImageBitmap(bmp);
Log.i("Inside", "PICK_IMAGE");
}
if (requestCode == PICK_IMAGE_FROM_GALLERY) {
UriselectedImage= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.d("data",filePathColumn[0]);
Cursorcursor= getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
StringpicturePath= cursor.getString(columnIndex);
cursor.close();
imgView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Log.i("Inside", "PICK_IMAGE_FROM_GALLERY");
}
}
}
}
XML File:-
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#f0f0f0"><TextViewandroid:id="@+id/lblSelectOptions"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginTop="20dp"android:text="@string/two_options"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#ff0000"/><Buttonandroid:id="@+id/btnPhotoCamera"android:layout_width="75dp"android:layout_height="wrap_content"android:layout_below="@+id/lblSelectOptions"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:text="@string/camera"/><Buttonandroid:id="@+id/btnPhotoGallery"android:layout_width="75dp"android:layout_height="wrap_content"android:layout_below="@+id/btnPhotoCamera"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:text="@string/gallery"/><Buttonandroid:id="@+id/btnCancel"android:layout_width="120dp"android:layout_height="wrap_content"android:layout_below="@+id/btnPhotoGallery"android:layout_centerHorizontal="true"android:layout_marginTop="19dp"android:text="@string/cancel"/><TextViewandroid:id="@+id/lblDisplayImage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/btnCancel"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:text="@string/below_this_text_image_will_be_displayed"android:textAppearance="?android:attr/textAppearanceMedium"android:textColor="#000000"android:textSize="13dp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_below="@+id/lblDisplayImage"android:layout_centerInParent="true"android:layout_marginTop="10dp"android:gravity="bottom"><!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
--><ImageViewandroid:id="@+id/imgDisplayImage"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/lblDisplayImage"android:layout_centerInParent="true"android:contentDescription="@string/area_where_image_is_to_be_displayed" /><!-- </ScrollView> --></RelativeLayout></RelativeLayout>
ALso Modify the Android Manifest file as per your use with following:-
<manifest....
<uses-sdkandroid:minSdkVersion="3"android:targetSdkVersion="21" /><uses-permissionandroid:name="android.permission.CAMERA" /><uses-permissionandroid:name="android.permission.RECORD_VIDEO" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-featureandroid:name="android.hardware.camera"android:required="false" /><application..............
</application></manifest>
Solution 2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Photos_Text1" />
<Button
android:id="@+id/takePhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text="@string/Photos_Button1" />
<Button
android:id="@+id/pagePhotoUpload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="pagePhotoUpload"
android:text="@string/Photos_Button2" />
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.85"
android:src="@drawable/ic_action_search" />
</LinearLayout>
I was using
android:onClick="takePhoto"
however this was not devined in my java anywhere, I had fabricated "takePhoto"
Post a Comment for "Launch Android Camera Intent And Layout"