我正在使用这些代码在点击按钮上捕获和存储图像。
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
destination = new File(Environment
.getExternalStorageDirectory(),
preferences.getSelectedItem().getItemNo() + "_"
+ preferences.getSelectedItem().getChasisNo() + "_up");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, TAKE_PHOTO_CODE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
FileInputStream in;
try {
in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imgPath = destination.getAbsolutePath();
// Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Image saved", "Image saved at " + imgPath);
upItem.setUpPhotoURL(String.valueOf(imgPath));
isPhotoAttached = true;
}
}
但是当我拍摄照片并尝试确认时,应用程序什么都不做。取消和重新选择选项工作完美,但确认拍摄的图像不起作用。谁能指出我问题出在哪里?
答案 0 :(得分:0)
在您的清单文件中添加了此权限
<uses-feature android:name="android.hardware.camera" />
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
String[] projection = { MediaStore.Images.Media.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Toast.makeText(this, capturedImageFilePath, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
试试这个
String mCurrentPhotoPath = destination.getAbsolutePath();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_CODE: {
if (resultCode == RESULT_OK) {
if (mCurrentPhotoPath != null) {
getBitmap(mCurrentPhotoPath);
}
}
break;
}
default:
break;
}
}
private Bitmap getBitmap(String mCurrentPhotoPath) {
/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */
/* Get the size of the ImageView */
int targetW = imageView.getMeasuredWidth();
int targetH = imageView.getMeasuredHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Bitmap oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 3;
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
return oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
bmOptions);
}
清单文件中的此权限
<uses-feature android:name="android.hardware.camera" />
答案 2 :(得分:0)
我认为这不起作用,因为图片的名称太大了。我改变了这个
destination = new File(Environment.getExternalStorageDirectory(),
preferences.getSelectedItem().getItemNo() + "_"
+ preferences.getSelectedItem().getChasisNo() + "_up");
到这个
destination = new File(Environment.getExternalStorageDirectory(),
preferences.getSelectedItem().getChasisNo() + ".jpg");
我不确定图像名称是否过大,但这种变化对我有用