我一直在尝试拍照并将其发送到我的Android应用中的服务器。我知道这个问题在这里被多次询问过,但是我无法找到将解决方案正确地集成到我的代码中的方法,因为它看起来像;所有如果我拍的照片都是低质量的(50K,我理解的缩略图?)。我的代码如下:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
try {
File file = createImageFile();//new File(getImageFileName());
FileOutputStream fOS = new FileOutputStream(file);
//TODO TEST PNG OR JPEG
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOS );
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
AsyncTaskHTTP asyncTaskHTTP = new AsyncTaskHTTP("Image");
asyncTaskHTTP.execute();
}
catch (Exception e){
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "User" + ID + "Image" + imageCounter + "_" + timeStamp;
imageCounter++;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String path = storageDir.getPath() + "/ALUMA";
File temp = new File(path);
temp.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".png", /* suffix */
temp /* directory */
);
currentImagePath = image.getPath();
return image;
}
我看到我需要在某处添加文件URI?或者是否有一种更简单的方法(我尝试添加文件&u?但是当我试图拍照时,应用程序只是崩溃了)?
答案 0 :(得分:1)
你得到的是小缩略图。使用以下代码获取高分辨率图像。你必须在打开相机意图之前传递文件路径。
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
}
您可以在ActivityResult
上检索如下图像if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) {
Toast.makeText(getActivity().getApplicationContext(), "Processing Image, Please Wait.", Toast.LENGTH_LONG).show();
if(mCurrentPhotoPath != null){
Log.d("onActivityResult", "Inside Image1");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options);
}
}else{
//Toast.makeText(getActivity().getApplicationContext(), "mCurrentPhotoPath is Null", Toast.LENGTH_SHORT).show();
}
}
mCurrentPhotoPath是文件生成传递给摄像机意图的路径