这是我现有的作物意图代码。这适用于所有设备
private void choosePhotoFromCamera() {
System.out.println("choosePhotoFromCamera");
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (getExternalFilesDir(null) == null) {
Log.e(TAG, "No SDcard");
Log.e(TAG,
"Maybe this will work : "
+ Environment.getExternalStorageDirectory());
Toast.makeText(ChooseFromCamera.this, "The app needs SDcard inserted",
Toast.LENGTH_LONG).show();
return;
}
File imagFile = null;
try {
imagFile = File.createTempFile("" + System.currentTimeMillis(),
".jpg", getExternalFilesDir(null));
c= imagFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
uriForTempCapturePhoto = Uri.fromFile(imagFile);
String uristring= getPath(this, uriForTempCapturePhoto);
uriForTempCapturePhoto= Uri.parse(uristring);*/
i.putExtra(MediaStore.EXTRA_OUTPUT, uriForTempCapturePhoto);
startActivityForResult(i, TAKE_PHOTO_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
System.out.println("onActivityResult3333333333333333333333");
if (requestCode == TAKE_PHOTO_REQUEST_CODE
|| requestCode == SELECT_PHOTO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = null;
if (requestCode == SELECT_PHOTO_REQUEST_CODE) {
uri = intent.getData();
} else if (requestCode == TAKE_PHOTO_REQUEST_CODE) {
uri = uriForTempCapturePhoto;
}
if (uri != null) {
try {
u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), c, null, null));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
final Intent intent1 = new Intent(
"com.android.camera.action.CROP");
System.out.println(" uri of the image "+uriForTempCapturePhoto);
intent1.setDataAndType(u, "image/*");
intent1.putExtra("" +"", "true");
intent1.putExtra("aspectX", 1);
intent1.putExtra("aspectY", 1);
intent1.putExtra("outputX", 100);
intent1.putExtra("outputY", 100);
intent1.putExtra("return-data", true);
startActivityForResult(intent1, CUT_PHOTO_REQUEST_CODE);
}
}else{
this.finish();
}
} else if (requestCode == CUT_PHOTO_REQUEST_CODE
|| requestCode == TAKE_GALLERY_REQUEST_CODE) {
if (resultCode == RESULT_OK && intent != null) {
System.out.println("4444444444444444444444444444444");
Bitmap bm = intent.getParcelableExtra("data");
if(bm != null){
System.out.println("Test : " + bm.toString());
}
else
System.out.println("Null Value");
Bitmap bmpCircular = createCircularBitmap(bm);
System.out.println("bmpCircular1111111111111111111"+bmpCircular);
/*if (bmpCircular == null) {
return;
}
String name = null;
String path = Utils.saveBitmap(
ChooseFromCamera.this.getApplicationContext(), name,
bmpCircular);
if (ChooseFromCamera.this == null) {
Log.e(TAG, "called this at wrong time.");
return;
}
if (path != null) {
// sucessfully
System.out.println("choosePhotoFromGallery1111111111111111111"+path);
setContentView(R.layout.main);
ivCamera = (ImageView) findViewById(R.id.iv_camera);
ivAccount = (RoundCornerImageView) findViewById(R.id.iv_account_indicator);
ivAccount.setImageBitmap(bmpCircular);
ivCamera.setVisibility(View.INVISIBLE);
}*/
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmpCircular.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
//String encoded = "abcdef";
System.out.println("encoded value"+encoded);
ChooseCamera.callKonyFunction(encoded);
this.finish();
}else{
this.finish();
}
}
}
现在当我在 android M 中测试它时,我得到了以下异常。
java.lang.IllegalArgumentException: mediaStoreUri 必须是 MediaStore Uri
要解决此问题,请使用 mediastore 插入图像并尝试使用下面的 uri,但此代码也仅在 android M 中失败。请帮助修复。
if (uri != null) {
try {
u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), c, null, null));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{}
intent1.setDataAndType(u, "image/*");
现在我只遇到 Android M 设备的问题。在这种情况下,裁剪意图返回 null