我正在开发一个Android应用程序,它将图像从相机或设备照片库上传到远程站点.后者我工作正常,我可以选择并上传.但是,我无法拍摄完整尺寸的图片并上传.这是我的代码:
// From onCreate Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
我有一个方法来处理Activity结果.这两个都处理从图库和相机中选择:
public void onActivityResult(int requestCode, int resultCode, Intent data) { String filepath = ""; Uri selectedImageUri; if (resultCode == RESULT_OK) { if (requestCode == CAMERA_PIC_REQUEST) { Bitmap photo = (Bitmap) data.getExtras().get("data"); // Gets real path of image so it can be uploaded selectedImageUri = getImageUri(getApplicationContext(), photo); } else { selectedImageUri = data.getData(); } // Handle the upload ... } } public Uri getImageUri(Context inContext, Bitmap inImage) { String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); }
但是,这有效,图像很小,不符合存储图像的命名约定.我已经读过要保存完整大小我需要使用putExtra并在cameraIntent上传递MediaStore.EXTRA_OUTPUT并声明一个临时文件,所以我调整了我的意图代码如下:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss"); camImgFilename = sdf.format(new Date())+".jpg"; File photo = new File Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), camImgFilename); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
然而,这会导致抛出错误,指出"未找到来源".
不知道从哪里开始?
UPDATE
看来我的照片正在创作中.应用程序关闭后,我可以使用文件资源管理器导航到目录并查看图像.我不确定导致应用程序崩溃的原因.
我认为问题是你仍然试图从中访问缩略图Intent
.要检索完整大小的图像,您需要直接访问该文件.所以我建议在开始摄像机活动之前保存文件名,然后再加载文件onActivityResult
.
我建议您阅读官方文档中的" 拍摄照片"页面,您可以在其中找到有关缩略图的引用:
注意:来自"数据"的缩略图图像可能适用于图标,但不是更多.处理完整尺寸的图像需要更多的工作.
同样在最后一节中,您将找到所需的代码:
private void setPic() { // Get the dimensions of the View int targetW = mImageView.getWidth(); int targetH = mImageView.getHeight(); // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min(photoW/targetW, photoH/targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); mImageView.setImageBitmap(bitmap); }