活动 - 空对象引用上的'boolean android.database.Cursor.moveToFirst()'

时间:2018-02-02 09:17:46

标签: android image android-intent cursor gallery

就像你看到的标题一样,我在一个空对象引用上遇到了'boolean android.database.Cursor.moveToFirst()'的大麻烦... 已经阅读了许多关于该问题的帖子,但它们都没有解决我的问题。它在两周前工作,我不会改变任何东西,只是将AndroidStudios更新到新版本,之后会出现这种错误。

这是我的代码

CircleImageView civ;
private final static int RESULT_SELECT_IMAGE = 100;
private static final String TAG = "GalleryUtil";
BitmapFactory.Options bmOptions;
String picturePath;
ImageView imageView;

        civ.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
            Intent GI = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(GI,RESULT_SELECT_IMAGE);
        }catch (Exception e){
                e.printStackTrace();
            }

        }
    });

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_SELECT_IMAGE && resultCode == Activity.RESULT_OK && data !=null && data.getData() != null) {
        try {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();


            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex); // Bład
            cursor.close();

            BitmapFactory.decodeFile(picturePath, bmOptions);

            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;
            imageView = findViewById(R.id.circle_profile);
            int targetW = imageView.getWidth();
            int targetH = imageView.getHeight();
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;


            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath, bmOptions));
        }catch (Exception e){
            e.printStackTrace();
            Intent returnFromGalleryIntent = new Intent();
            setResult(RESULT_CANCELED, returnFromGalleryIntent);
            finish();
        }
    }else{
        Log.i(TAG,"RESULT_CANCELED");
        Intent returnFromGalleryIntent = new Intent();
        setResult(RESULT_CANCELED, returnFromGalleryIntent);
        finish();
    }
}

它的作用很快,我得到了像这样的圆形图像视图

    <de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:layout_column="0"
    android:layout_row="7"
    android:id="@+id/circle_profile"
    android:src="@drawable/profile"
    app:civ_border_color="@color/civ"
    app:civ_border_width="2dp"
    android:layout_margin="8dp"
    />

当我点击默认照片时它会向我的画廊显示我拍摄的一些照片,然后当我按下其中一张时, bum 我崩溃了:光标为空并且已经尝试修复它,但没有任何好的问题

我是Android的新手,所以请不要告诉我这个链接或者这个有解决方案,需要明确的答案才能理解

///更新///

if (requestCode == RESULT_SELECT_IMAGE && resultCode == Activity.RESULT_OK && data !=null && data.getData() != null) {
        try {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(String.valueOf(data.getData()), bmOptions);


            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;
            imageView = findViewById(R.id.circle_profile);
            int targetW = imageView.getWidth();
            int targetH = imageView.getHeight();
            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;


            imageView.setImageBitmap(BitmapFactory.decodeFile(String.valueOf(data.getData()), bmOptions));
            //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath, bmOptions));

        }catch (Exception e){
            e.printStackTrace();
            Intent returnFromGalleryIntent = new Intent();
            setResult(RESULT_CANCELED, returnFromGalleryIntent);
            finish();
        }
    }else{
        Log.i(TAG,"RESULT_CANCELED");
        Intent returnFromGalleryIntent = new Intent();
        setResult(RESULT_CANCELED, returnFromGalleryIntent);
        finish();
    }

1 个答案:

答案 0 :(得分:0)

BitmapFactory.decodeFile(picturePath, bmOptions);

更改为

InputStream is = getContentResolver().openInputStream(data.getData());
BitmapFactory.decodeStream(is, bmOptions);

取消该Cursor以获取文件路径。