选择文件时出现空指针异常

时间:2015-02-14 07:27:02

标签: java android-intent nullpointerexception

我正在编写一个联系表单,我在其中创建一个带按钮的文件选择器,但问题是如果文件大小很大,不幸的是它会停止,如果没有文件浏览器而不是它给出空指针异常。 这是意图代码

Intent intent= new Intent(Intent.ACTION_PICK);

            intent.setAction(Intent.ACTION_GET_CONTENT);// its optional
            intent.setType("image/*");
            final int SELECT_IMAGE = 1;
            startActivityForResult(Intent.createChooser(intent, "Select  Picture"), SELECT_IMAGE);
and here is the code of on activity result
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {

           if (requestCode == 1 && resultCode == RESULT_OK ) {


                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]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

     //this code converts image in base64 and the string is to be sent along post method:
                yourSelectedImage = BitmapFactory.decodeFile(filePath);
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    byte[] imageBytes = baos.toByteArray();
                    encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);


                } catch (Exception e) {
                    Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
                }


            } else {
                Toast.makeText(this, resultCode, Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }

    }

0 个答案:

没有答案