I am going to implement camera feature for my android app.for this, I searched a lot on internet and found 2 way to implement camera feature
-
design your own camera class using
android.view.SurfaceHolder and
android.view.SurfaceView
but this approach does look good to me as in this i have to take care of many things which will make my code complex.
2. I can use intent to call device camera. now again in this , i have 2 option
i ) using MediaStore.EXTRA_OUTPUT and not using MediaStore.EXTRA_OUTPUT
if i dont use MediaStore.EXTRA_OUTPUT I get a thumbnail which is not my actual requirement. i need image larger than this so if I scale this thumbnail, i find low quality image.
if i use MediaStore.EXTRA_OUTPUT I dint get any image on some device like samsung as in
onActivityResult(int requestCode, int resultCode, Intent intent)
I find intent is null . after searching a lot on internet, i found that this is device specific issue. i have Samsung Galaxy with me. and on this i am facing this issue.
i dont want my code to be a device specific code. so can any one tell me how to make my code generic. for your analysis, i am using following code
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
File root = new File(Environment
.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator)
if(!root.exists()){
root.mkdirs();
}
sdImageMainDirectory = new File(root, "myPicName");
startCameraActivity();
}
catch (Exception e) {
finish();
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
}
Uri outputFileUri;
protected void startCameraActivity() {
outputFileUri = Uri.fromFile(sdImageMainDirectory);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (resultCode) {
case 0:
finish();
break;
case -1:
try {
if(intent != null)
StoreImage(this, Uri.parse(intent.toURI()),
sdImageMainDirectory);
} catch (Exception e) {
e.printStackTrace();
}
Intent i = new Intent(this,Home.class);
i.putExtra("url", outputFileUri);
startActivity(i);
finish();
}
}
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
Bitmap bm = null;
try {
bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
FileOutputStream out = new FileOutputStream(imageDir);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Please check this link: Capture image using Intent
This code works fine in all the Samsung devices I have used (like Galaxy S, Galaxy Tab, Galaxy Pro).