Taking A High Quality Image Through Default Camera Activity And Saving It O The Sd Card
I am taking a high resolution picture through the default camera activity(using intent.put Extras),and saving it to the sd card, Code: public class CameraActivity extends Activity
Solution 1:
Use following to achieve this.
Before you call CameraIntent create a file and uri based on that filepath as shown here.
filename = Environment.getExternalStorageDirectory().getPath() + "/test/testfile.jpg";
imageUri = Uri.fromFile(newFile(filename));
// start default cameraIntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult (cameraIntent, CAMERA_PIC_REQUEST);
Now, you have the filepath you can use it in onAcityvityResult method as following,
protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != CAMERA_PIC_REQUEST || filename == null)
return;
ImageViewimg= (ImageView) findViewById(R.id.image);
img.setImageURI(imageUri);
}
Solution 2:
This smells like outOfMemoryException. Instead of just fetching the huge picture file directly you need to do some code magic so that it doesn't eat up all the memory. Check out some documentation here: http://developer.android.com/training/displaying-bitmaps/index.html
And some code 4 u:
public Bitmap decodeFile(File f, int size){
try {
//Decode image size
BitmapFactory.Optionso=newBitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(newFileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<size) // || height break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Optionso2=newBitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(newFileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
returnnull;
}
Solution 3:
Try to put all the code in Respective Try Catch Exception handling. Then debug the code and check where the exception is thrown. You might be running out of memory exception.
Follow this:
http://developer.android.com/training/camera/photobasics.html
Solution 4:
publicclassMainActivityextendsAppCompatActivity {
privatestaticfinalintCAMERA_REQUEST=1888;
private ImageView imageView;
private LinearLayout view;
private TextView photoButton, shareButton, saveButton;
private Bitmap b, photo;
privatestaticfinalintMY_CAMERA_PERMISSION_CODE=100;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.view);
imageView = findViewById(R.id.image);
photoButton = this.findViewById(R.id.captur);
photoButton.setOnClickListener(newView.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)@OverridepublicvoidonClick(View v) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(newString[]{Manifest.permission.CAMERA},
MY_CAMERA_PERMISSION_CODE);
} else {
IntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNullint[] 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();
IntentcameraIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//convert bitmap
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
}
}
}
Post a Comment for "Taking A High Quality Image Through Default Camera Activity And Saving It O The Sd Card"