Android 安卓从相机获取全尺寸图像
我正在开发一个Android应用程序,它可以从相机或设备照片库上传图像到远程站点。后者我的工作很好,我可以选择和上传。然而,我有麻烦采取一个完整的大小图像和上传。这是我的代码:Android 安卓从相机获取全尺寸图像,android,image,image-processing,bitmap,android-camera-intent,Android,Image,Image Processing,Bitmap,Android Camera Intent,我正在开发一个Android应用程序,它可以从相机或设备照片库上传图像到远程站点。后者我的工作很好,我可以选择和上传。然而,我有麻烦采取一个完整的大小图像和上传。这是我的代码: // From onCreate Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 我
// From onCreate
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
我有一个方法来处理活动结果。这将处理从“多媒体资料”和“相机”中进行选择的两个操作:
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);
}
这是可行的,但是图像很小,并且不遵循存储图像的命名约定。我已经了解到,为了保存完整大小,我需要在cameraIntent上使用putExtra并传递MediaStore.EXTRA_输出,并声明一个临时文件,因此我调整了我的意图代码,如下所示:
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);
但是,这会导致抛出一个错误,声明“找不到源”
不知道从这里开始往哪里走
更新
看起来我的照片正在创建中。关闭应用程序后,我可以使用文件浏览器导航到该目录并查看图像。我不确定是什么原因导致应用程序崩溃。我认为问题在于您仍在尝试从
意图
访问缩略图。为了检索完整大小的图像,您需要直接访问该文件。因此,我建议在启动相机活动之前保存文件名,然后在激活结果上加载文件
我建议您阅读以下关于缩略图的引用:
注意:这个来自“数据”的缩略图像可能适合作为图标,但不是更多。处理全尺寸图像需要更多的工作
在最后一节中,您将找到所需的代码:
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);
}
检查这个,您可以实现自定义照相机。感谢您的回答,删除位图照片=(位图)数据。getExtras().get(“数据”);已停止我的应用程序崩溃,并且上面列出的galleryAddPic()方法setPic()会列出Gallery中的图像。完美的