Can';t从android摄像头上传图像捕获
我正在尝试编写一个小代码,允许我在从相机拍摄照片后直接发送照片,我想从相机拍摄发送pict,但从未成功,我总是收到消息“出了问题” 这是密码Can';t从android摄像头上传图像捕获,android,android-camera,android-camera-intent,Android,Android Camera,Android Camera Intent,我正在尝试编写一个小代码,允许我在从相机拍摄照片后直接发送照片,我想从相机拍摄发送pict,但从未成功,我总是收到消息“出了问题” 这是密码 public void loadImagefromGallery(View view) { CharSequence colors[] = new CharSequence[] {"Galery", "Foto"}; AlertDialog.Builder builder = new AlertDialog.Buil
public void loadImagefromGallery(View view) {
CharSequence colors[] = new CharSequence[] {"Galery", "Foto"};
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
builder.setTitle("Pilih");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
} else if (which == 1) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if(requestCode == CAMERA_REQUEST){
Bitmap photo = (Bitmap) data.getExtras().get("data");
RoundedImageViewUtil imgView = (RoundedImageViewUtil) findViewById(R.id.profile);
imgView.setImageBitmap(photo);
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgPath));
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
params.put("filename", fileName);
} else if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
RoundedImageViewUtil imgView = (RoundedImageViewUtil) findViewById(R.id.profile);
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgPath));
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
params.put("filename", fileName);
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
Intead of
Bitmap photo = (Bitmap) data.getExtras().get("data");
试一试
然后从uri中获取图像位图。Intead of
Bitmap photo = (Bitmap) data.getExtras().get("data");
试一试
然后从uri获取图像位图。imgView.setImageBitmap(照片)中出现错误;您必须实现从imageuri获取位图的逻辑,这就是为什么它给出错误的原因;位图Bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);RoundedImageViewUtil imgView=(RoundedImageViewUtil)findViewById(R.id.profile);imgView.setImageBitmap(位图)
MediaStore.Images.Media.getBitmap()将为您提供缩略图;要访问全尺寸图像,您需要BitmapFactory,请参阅您需要使用您在上一条注释中添加的代码,该代码可以获取图像路径,您可以从中获取位图;您必须实现从imageuri获取位图的逻辑,这就是为什么它给出错误的原因;位图Bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);RoundedImageViewUtil imgView=(RoundedImageViewUtil)findViewById(R.id.profile);imgView.setImageBitmap(位图)MediaStore.Images.Media.getBitmap()将为您提供缩略图;要访问全尺寸图像,您需要BitmapFactory,请参阅您需要使用在上一条注释中添加的代码,该代码可以获取图像路径,从中可以获取位图。