I want to call system camera and take lots of photo in system view, and I also want to store those picture in some folders. I have search some codes just like below:
Intent imageCaptureIntent = new Intent("android.media.action.STILL_IMAGE_CAMERA");
File out = new File(Environment.getExternalStorageDirectory(), "camera"+System.currentTimeMillis()+".jpg");
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, 1);
this code can make me take lots of photo in system view, but it can't store the folder that I want(Environment.getExternalStorageDirectory()+ "camera"+System.currentTimeMillis()+".jpg").So, who have a good way to make sure my photo to store in the folder I want.Thank you advance!
In your onCreate() do this,
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA);
And in onActivityResult,
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != RESULT_OK)
return;
try {
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File tmpFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
FileOutputStream fos = new FileOutputStream(tmpFile);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException io_e) {
// TODO: handle error
}
}
in above code not create folder it store in default folder
but this code is store image in particulare folder
public class MainActivity extends ActionBarActivity {
Button btnImage;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
btnImage= (Button) findViewById(R.id.buttonCapture);
btnImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != RESULT_OK)
return;
try {
AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = imageAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"/Imagesss/");//Folder Name Imagesss
if (!root.exists()) {
System.out.println("No directory");
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".jpg" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException io_e) {
// TODO: handle error
}
//Display Data In Image View
if (requestCode == 0 && resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.