拍照并储存
1、使用Contentprovider和Intent
http://www.cnblogs.com/android-html5/archive/2011/12/18/2533962.html
2、多媒体的获取
http://blog.youkuaiyun.com/xingtian713/article/details/6525411
3、拍照并获取,采用OpenInputStream(Uri)
http://blog.youkuaiyun.com/chenjie19891104/article/details/6320323
4、检索android图片库
http://blog.youkuaiyun.com/chenjie19891104/article/details/6320664
5、获取
http://hi.baidu.com/knuuy/item/96edab1d5c72258c89a9566b
获取+检索
http://blog.youkuaiyun.com/asce1885/article/details/7082068
public class SizedCameraActivity extends Activity {
final static int CAMERA_RESULT = 0;
ImageView iv = null;
String imageFilePath = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置,储存路径
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/myfavoritepicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
//设置Intent
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//给Intent添加参数,保存到指定目录
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(it, CAMERA_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
iv = (ImageView) findViewById(R.id.ReturnedImageView);
//因为,拍照获得的图像的长宽,过大,倒是,ImageView放不下,所以需要根据屏幕大小比例调整,图像大小,来使ImageVIew显示
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
// Load up the image's dimensions not the image itself
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
//设置BitmapFactory的参数,只计算出图像的大小,而不实际进行图像的解码:
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
Log.v("HEIGHTRATIO", "" + heightRatio);
Log.v("WIDTHRATIO", "" + widthRatio);
//根据长宽中,较大的来设置比例
if ((heightRatio > 1) && (widthRatio > 1)) {
if (heightRatio > widthRatio) {
// Height ratio is larger, scale according to it
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
// Width ratio is larger, scale according to it
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
// Decode it for real
bmpFactoryOptions.inJustDecodeBounds = false;
//从sd目录中读取到Bitmap
bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
// Display it
iv.setImageBitmap(bmp);
}
}
}
几种获取到照相机的图片的方法
使用openInputStream(Uri),参考链接3
ContentValues values = new ContentValues();
Uri picUri=MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);//参考链接2
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath);
startActivityForResult(intent, RESULT_CODE);
onActivityResult{
BitmapFactory.Options op = new BitmapFactory.Options();
Bitmap pic = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(picUri),null,op);
}
使用cursor+query,参考链接5
ContentValues values = new ContentValues();
photoUri=this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
onActivityResult{
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(photoUri, null, null, null, null);
cursor.moveToFirst(); //游标移到第一条就是系统默认的路径
if (cursor != null) {
String getFilePath = cursor.getString(1); //获取到的路径可以用于图片自定义压缩了
cursor.close();
}
使用指定的目录来保存,直接保存
String imageFilePath =Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile);
Intent it=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(it, CAMERA_RESULT);
参考http://blog.youkuaiyun.com/asce1885/article/details/7081633
保存Bitmap
String path=Environment.getExternalStorageDirectory().toString();
//ff.png是将要存储的图片的名称
File file=new File(path, "ff.png");
//从资源文件中选择一张图片作为将要写入的源文件
try {
FileOutputStream out=new FileOutputStream(file);
bttt.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}