'对象引用上的'boolean android.database.Cursor.moveToFirst()'

时间:2017-02-26 01:52:53

标签: java android nullpointerexception gallery android-cursor

您好我有一个麻烦这个错误

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

1 个答案:

答案 0 :(得分:0)

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

<强>更新

启动选择意图

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