可能重复: I'm得到一个NullPointerException异常,当我使用ACTION_IMAGE_CAPTURE采取
图片我有一些code。
意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
档案照片=新的文件(CamDir,文件名);
imageUri = Uri.fromFile(照片);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(照片));
startActivityForResult(意向,0);
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
点阵位图= NULL;
如果(结果code == Activity.RESULT_OK和放大器;&安培;请求code == 0){
乌里selectedImage = imageUri;
ContentResolver的CR = getContentResolver();
位= android.provider.MediaStore.Images.Media.getBitmap(CR,selectedImage);
}
1,手机垂直位置。 2.启动应用程序。 3. preSS按钮来拍摄照片。照片 4. preSS确定。 (保存照片) 一切都很好。
1,手机垂直位置。 2.启动应用程序。 3. preSS按钮来拍摄照片。照片 4.旋转手机的水平位置。 5,preSS确定。 (保存照片) 有误差
E / AndroidRuntime(22779):java.lang.RuntimeException的:失败交付结果
ResultInfo {谁= NULL,请求= 0,结果= -1,数据= NULL}到活动
com.photo/com.photo.PhotoActivity}:
显示java.lang.NullPointerException
我认为当我旋转手机水平位置的意图被重新加载,并且相机不能 知道在哪里发送结果。 如何解决这个问题。
onActivityResult(...){
...
重装()
}
公共无效重装()
{意向意图= getIntent(); overridePendingTransition(0,0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
完();
overridePendingTransition(0,0);
startActivity(意向);
}
解决方案
公共类BrowsePicture延伸活动{
私有静态最终诠释SELECT_PICTURE = 1;
私人字符串selectedImagePath;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
((按钮)findViewById(R.id.Button01))
.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
//中的onCreate或任何情况下,你的希望用户
//选择一个文件
意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(意向,
选择图片),SELECT_PICTURE);
}
});
}
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(结果code == RESULT_OK){
如果(要求code == SELECT_PICTURE){
乌里selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
公共字符串getPath(URI URI){
的String []投影= {MediaStore.Images.Media.DATA};
光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
INT与Column_Index =光标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}
}
Possible Duplicate: I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture
I have some code.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(CamDir, filename);
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, 0);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
Uri selectedImage = imageUri;
ContentResolver cr = getContentResolver();
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
}
1.the phone in vertical position. 2.start the application. 3.press the button to take a photo. 4.press Ok. (save photo) Everything fine.
1.the phone in vertical position. 2.start the application. 3.press the button to take a photo. 4.rotate the phone to horizontal position. 5.press Ok. (save photo) Have error
E/AndroidRuntime(22779): java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=0, result=-1, data=null} to activity
com.photo/com.photo.PhotoActivity}:
java.lang.NullPointerException
I think when I rotate the phone to horizontal position intent was reloaded, and camera not know where to send results. How to fix this problem.
onActivityResult(...){
...
reload()
}
public void reload()
{ Intent intent = getIntent(); overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
解决方案
public class BrowsePicture extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}