点击活动A时,按钮会调用相机。我在活动B中写过相机代码。 Inshort A正在调用B将图片存储在本地文件中并返回活动A. 这是代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory() + "/X",
filename);
Uri uri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, ACTION_TAKE_PHOTO_B);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK
&& requestCode == ACTION_TAKE_PHOTO_B) {
StoreByteImage(getApplicationContext(), 90);
}
Intent ii = new Intent(CameraActivity.this, Receipt.class);
String count = "1";
startActivity(ii);
}
public static boolean StoreByteImage(Context mContext, int quality) {
// File sdImageMainDirectory = new File("/sdcard");
File sdCard = Environment.getExternalStorageDirectory();
File sdImageMainDirectory = new File(sdCard.getAbsolutePath() + "/X");
FileOutputStream fileOutputStream = null;
// String nameFile;
try {
byte imageData[] = null;
imageData = FileUtils.readFileToByteArray(sdImageMainDirectory);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
fileOutputStream = new FileOutputStream(
sdImageMainDirectory.toString() + filename);
/*-
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, quality, bos);*/
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
myImage.compress(CompressFormat.JPEG, 60, bos1);
byte[] data = bos1.toByteArray();
up_image = Base64.encodeToString(data, 0);
uploadImage(up_image, filename);
bos1.flush();
bos1.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
问题是startActivityForResult立即触发onActivityResult并且resultCode获得值0,其中RESULT_OK具有-1(其右边不是它)。 当我读到几篇文章时,我遇到了singleInstance / singleTop确实取消了您的活动。我是Android的新手,有些人可以对此有所了解。