照片捕获 Intent 仅在三星手机上导致 NullPointerException

发布于 2024-11-29 15:51:31 字数 798 浏览 2 评论 0原文

仅在三星手机上,照片拍摄 Intent 会导致 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);
    }
}

Photo capture Intent causes NullPointerException on Samsung phones only.

Implementation below.

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两仪 2024-12-06 15:51:31

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

但是,在非三星手机上使用此修复程序会返回错误的图像,因此我会使用

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

I found a fix (not my work) that makes it work for Samsung devices. The blog with explanation can be found here.

However, using this fix on non-Samsung phones returns the wrong image, so I would use an

if(imageURI != null) {
    // do it the normal way
else {
    // do it the "Samsung" way
}
过期情话 2024-12-06 15:51:31

刚刚在三星 S4 上遇到了同样的问题,发现将 configChanges 添加到 AndroidManifest.xml 解决了问题:

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

Just got the same issue on a Samsung S4 and found out that adding configChanges to the AndroidManifest.xml solved the problem:

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" >
</activity>
浅浅 2024-12-06 15:51:31

你可以在这里查看一个简单的方法来获取 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路径捕获图像

获取相机捕获图像路径安卓

you can check a little simple way over here to get Uri.

Get camera capture image path in android

calling camera

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

on result activity

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();
}

Then you can
get Uri path capture image

Get camera capture image path in android

橙味迷妹 2024-12-06 15:51:31

在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 路径捕获图像

Get camera capture image path in android

calling camera

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

on result activity

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();

then u can
get Uri path capture image

(source)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文