Capture the image and save it to SD card in a new folder
I call the camera and capture the images, I will have to record 10 images one by one and store them on the SD card before I can set them as an image. Please check my code below, it is not set to image.
How can I save it to SD card and get it to set the image? How can I name the images before saving?
In the first activity, I call the camera:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
init();
}
private void init() {
String extStorageDirectory = Environment.getExternalStorageDirectory()
+ "/testing";
File xmlDirectory = new File(extStorageDirectory);
if (!xmlDirectory.exists())
xmlDirectory.mkdirs();
iv1 = (ImageView) findViewById(R.id.iv1);
}
private OnClickListener onBtnClicked = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case PHOTO:
Intent selectImageIntent = new Intent(first.this,
second.class);
startActivityForResult(selectImageIntent, 1);
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
Log.d("*****************",
"inside onactivityresult in main activity=" + result);
Bitmap bitmap = BitmapFactory.decodeFile(result);
iv1.setImageBitmap(bitmap);
iv1.setScaleType(ScaleType.FIT_XY);
}
}
}
And in my second activity, I grab the image and pass it to the first activity:
private void init() {
picturePath = Environment.getExternalStorageDirectory() + "/Camera/"
+ "test.jpg";
System.out.println("thumbnail path~~~~~~" + picturePath);
File file = new File(picturePath);
outputFileUri = Uri.fromFile(file);
}
public void startCamera() {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", picturePath);
setResult(RESULT_OK, returnIntent);
finish();
}
}
}
source to share
It is bad practice to initialize your object inside another method.
remove this line iv1 = (ImageView) findViewById(R.id.iv1);
from init()
and change your OnCreate () to the following path.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize here
iv1 = (ImageView) findViewById(R.id.iv1);
mContext = this;
init();
}
hope this helps you.
source to share
use this code and put the file name and path, the camera will capture the image and save it with the given name
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (!APP_FILE_PATH_Media.exists())
{
APP_FILE_PATH_Media.mkdirs();
}
uriSavedImage =new File(APP_FILE_PATH_Media+ "/" +
"filename"+ ".jpeg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(uriSavedImage));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
in onActivityResult () use this code to set imageView
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bm = BitmapFactory.decodeFile(uriSavedImage.getAbsolutePath(), options);
}
catch(Exception ee)
{
}
source to share