· To access the camera by using intent We use MediaStore.ACTION_IMAGE_CAPTURE in Intent function.
Function
Intent(MediaStore.ACTION_IMAGE_CAPTURE).
startActivityForResult.
Coding Part:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/username"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:text="Click" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_below="@+id/button1"/>
</RelativeLayout>
Activity:
public class MainActivity extends Activity {
Button clickBtn;
String filePath, filename;
Bitmap yourSelectedImage;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickBtn = (Button) findViewById(R.id.button1);
image = (ImageView) findViewById(R.id.image);
clickBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// file path of captured image
filePath = cursor.getString(columnIndex);
// file path of captured image
File f = new File(filePath);
filename = f.getName();
Toast.makeText(getApplicationContext(),
"Your Path:" + filePath, 2000).show();
Toast.makeText(getApplicationContext(),
"Your Filename:" + filename, 2000).show();
cursor.close();
// Convert file path into bitmap image using below line.
// yourSelectedImage = BitmapFactory.decodeFile(filePath);
// Toast.makeText(getApplicationContext(),
// "Your image"+yourSelectedImage, 2000).show();
Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
// put bitmapimage in your imageviewyourSelectedImage
image.setImageBitmap(myBitmap);
image.setVisibility(View.VISIBLE);
// To save the file in sdcard
Savefile(filename, filePath);
}
}
}
public void Savefile(String name, String path) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/MyAppFolder/MyApp/");
File file = new File(Environment.getExternalStorageDirectory()
+ "/MyAppFolder/MyApp/" + name);
if (!direct.exists()) {
direct.mkdir();
}
if (!file.exists()) {
try {
file.createNewFile();
FileChannel src = new FileInputStream(path).getChannel();
FileChannel dst = new FileOutputStream(file).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Add these things in your android.manifest:
//Camera
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
//for sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
No comments:
Post a Comment