java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=131074,result=-1,data=Intent{(has extras)}}传递到活动
嗨,我正试图用相机上传图片,或者从图库上传图片,我下面的代码在棒棒糖设备上运行得很好,但是在果冻豆中出现了一个错误。它显示java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=131074,result=-1,data=Intent{(has extras)}}传递到活动,java,android,android-intent,android-camera-intent,Java,Android,Android Intent,Android Camera Intent,嗨,我正试图用相机上传图片,或者从图库上传图片,我下面的代码在棒棒糖设备上运行得很好,但是在果冻豆中出现了一个错误。它显示java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=131074,result=-1,data=Intent{(has extras)}}传递给活动 下面是我在onActivity结果中的代码 @ Override public void onActivityResult(int requestCode, i
java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=131074,result=-1,data=Intent{(has extras)}}传递给活动
下面是我在onActivity结果中的代码
@
Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_CANCELED) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE) {
fileUri = data.getData();
Log.d("gallery", "" + data.getData());
performcropg(fileUri);
} else if (requestCode == REQUEST_CAMERA) {
fileUri = data.getData();
performcrop(fileUri);
} else if (requestCode == PIC_CROP) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
} else if (requestCode == pic_crop) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
}
}
}
}
简短解决方案:在验证
数据时也添加此选项!=null&&data.getData()!=空
as
if(requestCode==REQUEST\u-CAMERA&data!=null&data.getData()!=null)
这不会使您崩溃,但您无法获得图像
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_CANCELED) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
fileUri = data.getData();
Log.d("gallery", "" + data.getData());
performcropg(fileUri);
} else if (requestCode == REQUEST_CAMERA && data != null && data.getData() != null) {
fileUri = data.getData();
performcrop(fileUri);
} else if (requestCode == PIC_CROP && data != null && data.getData() != null) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
} else if (requestCode == pic_crop && data != null && data.getData() != null) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
}
}
}
}
所有版本的完整演示。
public class MainActivity extends Activity {
private ImageView camera, gallery;
File photoFile;
Uri uri;
private final static int RESULT_LOAD_IMAGE = 1;
private final static int REQUEST_IMAGE_CAPTURE = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = (ImageView) findViewById(R.id.Img);
gallery = (ImageView) findViewById(R.id.imageView);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
photoFile = Environment.getExternalStoragePublicDirectory("/myimage/save.jpg");
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
uri = data.getData();
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
} else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
} else {
if (photoFile != null) {
String selecteadImage = photoFile.getAbsolutePath();
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
}
}
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
清单权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/image_1">
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
活动\u main:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/image_1">
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
简短解决方案:在验证
数据上也添加此选项!=null&&data.getData()!=空
as
if(requestCode==REQUEST\u-CAMERA&data!=null&data.getData()!=null)
这不会使您崩溃,但您无法获得图像
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_CANCELED) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE && data != null && data.getData() != null) {
fileUri = data.getData();
Log.d("gallery", "" + data.getData());
performcropg(fileUri);
} else if (requestCode == REQUEST_CAMERA && data != null && data.getData() != null) {
fileUri = data.getData();
performcrop(fileUri);
} else if (requestCode == PIC_CROP && data != null && data.getData() != null) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
} else if (requestCode == pic_crop && data != null && data.getData() != null) {
if (data != null) {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
saveToInternalStorage(selectedBitmap);
Bitmap circleBitmap = Bitmap.createBitmap(selectedBitmap.getWidth(), selectedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(selectedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(selectedBitmap.getWidth() / 2, selectedBitmap.getHeight() / 2, selectedBitmap.getWidth() / 2, paint);
img_role.setImageBitmap(circleBitmap);
}
}
}
}
}
所有版本的完整演示。
public class MainActivity extends Activity {
private ImageView camera, gallery;
File photoFile;
Uri uri;
private final static int RESULT_LOAD_IMAGE = 1;
private final static int REQUEST_IMAGE_CAPTURE = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = (ImageView) findViewById(R.id.Img);
gallery = (ImageView) findViewById(R.id.imageView);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
photoFile = Environment.getExternalStoragePublicDirectory("/myimage/save.jpg");
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
uri = data.getData();
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
} else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
} else {
if (photoFile != null) {
String selecteadImage = photoFile.getAbsolutePath();
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
}
}
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
清单权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/image_1">
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
活动\u main:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/image_1">
<ImageView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/camera"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
您还可以添加“if(data!=null)”复选框,以确保代码在所有奇数条件下都能正常工作。这可能会帮助您解决此问题
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST && data!=null ) {
...
}
}
有关这方面的更多详细信息,您可以参考,也可以添加检查“if(data!=null)”以确保代码在所有奇数条件下都能正常工作。这可能会帮助您解决此问题
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST && data!=null ) {
...
}
}
有关此操作的更多详细信息,请参阅清单中的“注释出摄影机权限”
<!--<uses-permission android:name="android.permission.CAMERA" />-->
在清单中注释掉摄像头权限
<!--<uses-permission android:name="android.permission.CAMERA" />-->
您测试过哪些设备???@abhilnair您的问题解决了吗?或者您有什么简短的解决方案?对于某些特定设备,问题仍然相同。我的代码在某些设备上运行良好,但在某些数据中得到了解决null@abhilnair你试过我的完整解决方案吗?你测试过哪些设备???@abhilnair你的问题解决了还是你有什么问题解决方案?对于某些特定的设备,问题仍然是一样的。我的代码在某些设备上工作得很好,但在某些设备上,数据会丢失null@abhilnair你尝试了我的全部解决方案?你成功解决了吗?面对同样的问题,你解决了吗?面对同样的问题。