2011-08-11 105 views
10

照片拍摄上只有三星手机Intent导致NullPointerException。下面照片拍摄意图导致NullPointerException异常三星手机只

实现。

final Button capture = (Button)findViewById(R.id.capture_button); 
capture.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

    } 
}); 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 

     Bitmap thumbnail = (Bitmap)data.getExtras().get("data"); 
     ImageView image = (ImageView)findViewById(R.id.photoResultView); 
     image.setImageBitmap(thumbnail); 
    } 
} 
+0

哪行引起了NPE? – Matt

+0

@Matt image.setImageBitmap(thumbnail);行缩略图为空 –

回答

13

我发现了一个修复程序(不是我的工作),使它适用于三星设备。有解释的博客可以找到here

然而,在使用非三星手机此修复程序返回错误的图像,所以我会用一个

if(imageURI != null) { 
    // do it the normal way 
else { 
    // do it the "Samsung" way 
} 
+4

为@Pyrodante建议“然而,在使用非三星手机此修复程序返回错误的图像,所以我会用一个”可以在任何一个请张贴的完整代码,它应该对三星和非三星的做工 – LMK

2

你可以查了一下简单的方式在这里得到URI。

android系统

呼吁的结果活动

获取摄像机捕获图像路径照相机

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(Intent.createChooser(cameraIntent,"Select Picture"), CAMERA_PIC_REQUEST1); 

final ContentResolver cr = getContentResolver();  
final String[] p1 = new String[] { 
    MediaStore.Images.ImageColumns._ID, 
    MediaStore.Images.ImageColumns.DATE_TAKEN 
}; 
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");  
if (c1.moveToFirst()) { 
    String uristringpic = "content://media/external/images/media/" +c1.getInt(0); 
    Uri newuri = Uri.parse(uristringpic); 
    Log.i("TAG", "newuri "+newuri);      
} 
c1.close(); 
} 

然后你就可以得到 URI路径捕获图像

在Android的

呼吁的结果活动3210

Get camera capture image path in android

-2

获取摄像机捕获图像路径照相机

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(Intent.createChooser(cameraIntent,"Select Picture"), CAMERA_PIC_REQUEST1); 

final ContentResolver cr = getContentResolver();  
final String[] p1 = new String[] { 
    MediaStore.Images.ImageColumns._ID, 
    MediaStore.Images.ImageColumns.DATE_TAKEN 
}; 

Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");  
if (c1.moveToFirst()) { 
    String uristringpic = "content://media/external/images/media/" +c1.getInt(0); 
    Uri newuri = Uri.parse(uristringpic); 
    Log.i("TAG", "newuri "+newuri); 
} 
c1.close(); 

那么在哪里 得到URI路径捕获图像

source

+0

Lone链接[被认为是一个不好的答案](http://stackoverflow.com/faq#deletion),因为它本身毫无意义,并且目标资源不能保证在将来活着。请尽量至少包含您要链接的信息摘要。 – j0k

3

刚坐上三星S4同样的问题,结果发现,添加configChanges在AndroidManifest.xml中解决了这个问题:

<activity 
    android:name=".YourActivity" 
    android:configChanges="orientation|keyboardHidden|screenSize" 
    android:screenOrientation="portrait" > 
</activity> 
+0

适合我!奇怪的三星! – Steven