1

I have tried following code in which i am trying to save my uploaded image into SD card folder with some name.It gives null pointer exception on the mentioned line. Any suggestions please.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

Code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(requestCode==i && resultCode==RESULT_OK && data != null){
    Uri selectedImage =  data.getData();
    coverpic.setImageURI(selectedImage);
    Bitmap  image =( (BitmapDrawable)coverpic.getDrawable()).getBitmap();//This line throws the exception
    if (!direct.exists()) {
    File wallpaperDirectory = new File("/images/");
    wallpaperDirectory.mkdirs();
     }
     Bundle extras = getIntent().getExtras();
     File file = new File(new File("/images/"),extras.getString("name")+i + ".jpg" );
            if (file.exists()) {
                file.delete();
            }
     try{
    FileOutputStream out=new FileOutputStream(file);
    image.compress(Bitmap.CompressFormat.JPEG,100,out);
    out.flush();
    out.close();
    }
catch(Exception e)
            {
                e.printStackTrace();
              }
        }

1 Answers1

1

Try this, this will resize and save the image to phone

Also make sure you have correct permissions in your manifest file.

Here i have also used Glide to get bitmap, i think that's any easy way and it wont give a nullpointer.

Inside the onActivityresult

 if(requestCode==i && resultCode==RESULT_OK && data != null){
    Uri selectedImage =  data.getData();
   select(selectedImage);

To find the file

  public void select(Uri selectedImage) {
            // TODO Auto-generated method stub
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor =getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath2 = cursor.getString(columnIndex);
            cursor.close();

                save(new File(picturePath2));

        }

To save file

public File save(final File file_) {
        // TODO Auto-generated method stub
        System.out.println("newfilepath come to resize");
        File dir = new File(Environment.getExternalStorageDirectory().getPath()
                + "/Images/");
        try {
            dir.mkdir();
        } catch (Exception e) {
            e.printStackTrace();

        }
        String filename = Environment.getExternalStorageDirectory().getPath()
                + "/Images/";
        File newfile = new File(filename);
        String Unedited_Img_Name = "myfile"
                + String.valueOf(System.currentTimeMillis()) + ".jpg";
        final File file = new File(newfile, Unedited_Img_Name);
        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {
                // TODO Auto-generated method stub
                Bitmap b = null;
                try {
                    b = Glide.with(getActivity()).load(file_).asBitmap().into(200,200)
                            .get();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                FileOutputStream fOut;
                try {
                    fOut = new FileOutputStream(file);
                    b.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    b.recycle();
                } catch (Exception e) { // TODO

                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                String newfilepath = file.getAbsolutePath();

                int file_size = Integer
                        .parseInt(String.valueOf(file.length() / 1024));
                file_size = Integer.parseInt(String.valueOf(file_.length() / 1024));
                if (file_size < 150) {
                    newfilepath = file_.getAbsolutePath();
                }

            }
        }.execute();
        return file;
    }
George Thomas
  • 4,566
  • 5
  • 30
  • 65