我想在我的应用中拍照。在模拟器上一切正常。 但是在平板电脑上,Intent会立即返回onActivityResult中的Activity.RESULT_CANCELED。图片保存在SD卡上。
以下是拍照的代码:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
String tmpFile = CommonFunctions.generateRandomFileName() + ".jpg";
String fileName = CommonFunctions.getNoticeSavePath() + tmpFile;
System.out.println("Filename " + fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(fileName)));
notice.setBild(tmpFile);
startActivityForResult(intent, RESULT_PICTURE);
onActivityResult的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_PICTURE:
System.out.println("ResultCode" + resultCode);
if (resultCode == Activity.RESULT_OK) {
}
else if (resultCode == Activity.RESULT_CANCELED){
notice.setBild("");
Toast.makeText(getBaseContext(), "Bild wurde nicht hinzugefügt", Toast.LENGTH_LONG).show();
}
break;
}
}
设置了权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
结果始终为RESULT_CANCELED,但照片正确存储在SD卡上。 问题是什么?
答案 0 :(得分:0)
做这样的事情
private static final int REQUEST_CAMERA = 1, SELECT_FILE = 2; //global
onresult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = null;
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 300, 200, true);
String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("endum_image_", f.toString()).commit();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE)
{
Uri selectedImageUri = data.getData();
//getRealPathFromURI(selectedImageUri);
String tempPath = getPath(selectedImageUri, Signup2Activity.this);
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("endum_image_", tempPath).commit();
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath,btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 300, 200, true);
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
}
}
imSex.setImageBitmap(bm);
flag= true;
}