之前採用的方式(返回的照片会被压缩,不能达到预期效果):
Intent intent = new Intent();
Intent intent_camera = getPackageManager().getLaunchIntentForPackage("com.android.camera");if (intent_camera != null) { intent.setPackage("com.android.camera");}intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, 100);回调:(在返回的data中区数据,然后保存,然后再拿到路径。这样图片是已经压缩了的)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("TAG", "requestCode" + requestCode + " ,resultCode" + resultCode+ data);if (requestCode == 100 && resultCode == Activity.RESULT_OK&& null != data) { // 照相返回界面String sdState = Environment.getExternalStorageState();if (!sdState.equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(getApplicationContext(), "未找到SDK",Toast.LENGTH_LONG).show();Log.d("TAG", "sd card unmount");return;}new DateFormat();String name = DateFormat.format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA))+ ".jpg";Log.d("TAG", "name.........." + name);Bundle bundle = data.getExtras();// 获取相机返回的数据,并转换为图片格式Bitmap bitmap;String filename = null;bitmap = (Bitmap) bundle.get("data");FileOutputStream fout = null;// 定义文件存储路径File file = new File("/sdcard/e_community/");if (!file.exists()) { file.mkdirs();}filename = file.getPath() + "/" + name;try { fout = new FileOutputStream(filename);// 对图片进行压缩bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout); // FileOutputStream outputStream = new FileOutputStream(filename); // 文件输出流 // outputStream.write(data); // 写入sd卡中 // outputStream.close(); // 关闭输出流 } catch (FileNotFoundException e) { e.printStackTrace();} finally { try { fout.flush();fout.close();} catch (IOException e) { e.printStackTrace();}}Log.d("TAG", "相片路径" + filename);}
解决图片太小的问题。就是在启动相机的时候设置好照片保存的路径以及名称,这样data中就不会返回数据而是null,这样就能够依据自己设置的路径已经设置的名称通过BitmapFactory.decodeFile(imageUrl.get(j),option);获取到照片。
启动代码例如以下:
private String camera_path = Environment.getExternalStorageDirectory().toString()+"/e_community/";保存的路径
private String camera_photo_name;//保存的名称
Uri mOutPutFileUri;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File path1 = new File(PhotoUtil.camera_path); if(!path1.exists()){ path1.mkdirs(); } camera_photo_name=System.currentTimeMillis()+".png"; File file = new File(path1,camera_photo_name); mOutPutFileUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutPutFileUri);startActivityForResult(intent, 111);
回调:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("TAG", "requestCode" + requestCode + " ,resultCode" + resultCode+ data);if (requestCode == 111&&resultCode==Activity.RESULT_OK) { // 照相返回界面String filename = PhotoUtil.camera_path+ "/" + camera_photo_name;Log.d("TAG", "相片路径" + filename);}
}