【问题标题】:'boolean android.database.Cursor.moveToFirst()' on a null object reference [duplicate]空对象引用上的'boolean android.database.Cursor.moveToFirst()'
【发布时间】:2017-07-16 18:32:06
【问题描述】:

您好,我遇到了这个错误的问题

Exception java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Camera/IMG_20160619_152633.jpg }} to activity {paradox.galopshop/paradox.galopshop.All}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference 

我想从手机图库中获取图片。我选择了一张照片并在 imageview 中查看它..

我不知道这是什么问题,因为在模拟器(genymotion)中一切正常

protected  void onImageViewClick(){
  //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
    TextView tw=(TextView)findViewById(R.id.addimage);
    tw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE );

        }
    });

}

protected  void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode==RESULT_OK){
        if(requestCode==SELECT_PICTURE){
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                // Get the path from the Uri
                String path = getPathFromURI(selectedImageUri);
                Log.i("IMAGE PATH TAG", "Image Path : " + path);
                // Set the image in ImageView
                ImageView imageView=(ImageView)findViewById(R.id.imageView2);
                imageView.setImageURI(selectedImageUri);

                TextView tw=(TextView)findViewById(R.id.addimage);
                tw.setText("Načítané");



            }
        }
    }
}

private String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

我的清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="adjustResize">
    <activity android:name=".All">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

感谢您的想法..

///// 更新

protected  void onImageViewClick(){
  //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
    TextView tw=(TextView)findViewById(R.id.addimage);
    tw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          /*  Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE ); */

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            startActivityForResult(intent, UPLOAD_RESULT_CODE);

        }
    });

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == All.RESULT_OK) {
        // get image from intent
        InputStream inputStream = null;
        try {
            inputStream = getApplication().getContentResolver().openInputStream(data.getData());

        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        inputStream.close();
        // Here you can decode buffer to a bitmap and show it in your image view
            Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
            ImageView imageView=(ImageView)findViewById(R.id.imageView2);
            Toast.makeText(this, bitmap.toString(), Toast.LENGTH_SHORT).show();
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java android nullpointerexception gallery android-cursor


    【解决方案1】:

    问题是您在 getPathFromURI() here 中得到了一个空游标,您可以找到为什么它可以为空。

    更新

    开始选择意图

    void uploadImage() { 
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*"); 
        startActivityForResult(intent, UPLOAD_RESULT_CODE); 
    }
    

    在此之后,您可以从这样的结果中获取图像文件

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        if (resultCode == MainActivity.RESULT_OK) { 
             // get image from intent 
            InputStream inputStream = mActivity.getContentResolver().openInputStream(data.getData()); 
            byte[] buffer = new byte[inputStream.available()]; 
            inputStream.read(buffer); 
            inputStream.close();
            // Here you can decode buffer to a bitmap and show it in your image view
        }
    }
    

    【讨论】:

    • 好的,我不知道如何解决我的问题.. 我有小米 Mi4,这是我的根手机上的问题,或者是我的代码中的问题..
    • 据我了解,您需要从记忆中挑选图像,对吧?
    • 是的,来自手机卡上硬件中的画廊。
    • 在 Genymotion 中运行良好,但在真实设备中却不行.. :( 没有错误什么也不做.. 画廊已关闭但 imageview 为空
    • 如果您尝试您的代码在设备和模拟器上运行良好?
    猜你喜欢
    • 2016-10-04
    • 2018-02-05
    • 2021-08-31
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多