error when selecting android gallery image in my annotation app

Asked

Viewed 159 times

0

inserir a descrição da imagem aquiGuys is the following, I have a very simple diary notes app in which has an option to select and attach a photo in the note, the photo can be taken at the same time with the camera or imported from the gallery, the problem is that when I will import from the gallery, only sure when importing through the "gallery" option that displays all the photos, but as you know, in the left menu of the gallery have options like "downloads", "recent images" and if I select some image through these error options , the app does not close but simply can not select the image, only works if I select specifically through the "gallery" option. Below is the error log in the application and the classes corresponding to the error:

public class Rounder {

    public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int width, int height) {
        int targetWidth = width;
        int targetHeight = height;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);

        Path path = new Path();

        float padding = 5;

        RectF rectF = new RectF( padding, padding, targetWidth-padding, targetHeight-padding);

        path.addRoundRect(rectF, (float)0.0*width, (float)0.0*width, Path.Direction.CCW);

        canvas.clipPath(path);

        Bitmap sourceBitmap = scaleBitmapImage;

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.parseColor("#FFFFFF"));
        paint.setStrokeWidth(padding+10);

        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                sourceBitmap.getHeight()), new Rect(0, 0, targetWidth-10,
                        targetHeight-10), paint);

        //canvas.drawCircle(targetWidth/2, targetHeight/2,targetHeight/2, paint);


        return targetBitmap;
    }

}



public class NewDiaryFragment extends Fragment {

    Button addPhotoButton, addAudioButton,playPauseButton,restartPlayButton,deleteAudioButton,deletePhotoButton,
        addDiaryButton, cancelButton, datePickerButton;
    ImageView imageView;
    EditText textEditText;

    OneDiary oneDiary;
    Bitmap bitmap;
    String audioPath,photoPath;
    Date date;
    Uri mImageCaptureUri;

    static final int REQUEST_DATE = 0;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final int PICK_FROM_FILE = 2;

    AudioRecorder audioRecorder;
    AudioPlayer audioPlayer;

    Boolean isPlaying, isRecording;
    DiaryDB diaryDB;

    DateFormat[] df = new DateFormat[] {
            DateFormat.getDateInstance(DateFormat.LONG, Locale.US),
            DateFormat.getDateTimeInstance(),
            DateFormat.getTimeInstance(),
    };

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.new_diary, null);
        addAudioButton = (Button) v.findViewById(R.id.record_audio_button);
        addPhotoButton = (Button) v.findViewById(R.id.take_photo_button);
        deleteAudioButton = (Button)v.findViewById(R.id.cancel_audio_button);
        deletePhotoButton = (Button)v.findViewById(R.id.cancel_photo_taking_button);
        addDiaryButton = (Button)v.findViewById(R.id.add_diary_button);
        cancelButton = (Button)v.findViewById(R.id.cancel_new_diary_button);
        imageView = (ImageView)v.findViewById(R.id.photo_image_view);
        playPauseButton = (Button) v.findViewById(R.id.play_pause_button);
        restartPlayButton = (Button) v.findViewById(R.id.restart_button);

        textEditText = (EditText)v.findViewById(R.id.text_edit_text);
        datePickerButton = (Button)v.findViewById(R.id.date_picker_button);

        photoPath = "";

        date = new Date(System.currentTimeMillis());
        datePickerButton.setText(df[0].format(date));

        oneDiary = new OneDiary();
        diaryDB = new DiaryDB(getActivity().getApplicationContext());

        bitmap = BitmapFactory.decodeResource(getResources(),GetRandomPic.getRandomPic());
        imageView.setImageBitmap(Rounder.getRoundedShape(bitmap, 300, 300));

        final String [] items = new String [] {getActivity().getString(R.string.select_image_dialog_camera), getActivity().getString(R.string.select_image_dialog_SD)};
        ArrayAdapter<String> adapter  = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item,items);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(getActivity().getString(R.string.select_image_dialog_title));
        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) {
                if (item == 0) {
                    dispatchTakePictureIntent();
                } else {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
                }
            }
        } );

        final AlertDialog dialog = builder.create();

        addPhotoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
            }
        });


        audioRecorder = new AudioRecorder(getActivity().getApplicationContext());
        audioPlayer = new AudioPlayer(getActivity().getApplicationContext());
        audioPath = null;

        isPlaying = false;

        playPauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                audioRecorder.stopRecording();
                isRecording = false;
                addAudioButton.setBackgroundResource(R.drawable.record_icon);
                if (isPlaying) {
                    audioPlayer.pause();
                    playPauseButton.setBackgroundResource(R.drawable.play_icon);
                    isPlaying = !isPlaying;
                } else
                {
                    try {
                        audioPlayer.resume(audioPath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if(audioPath!=null) {
                        playPauseButton.setBackgroundResource(R.drawable.pause_play_icon);
                        isPlaying = !isPlaying;
                    }
                }

            }
            });

        restartPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                audioRecorder.stopRecording();
                isRecording = false;
                addAudioButton.setBackgroundResource(R.drawable.record_icon);
                if(audioPath!=null){
                    try {
                        audioPlayer.play(audioPath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    playPauseButton.setBackgroundResource(R.drawable.pause_play_icon);
                    isPlaying = true;
                }
            }
        });

        isRecording = false;



        addAudioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( isRecording ){
                    audioRecorder.stopRecording();
                    addAudioButton.setBackgroundResource(R.drawable.record_icon);
                } else{
                    audioPlayer.stop();
                    isPlaying = false;
                    playPauseButton.setBackgroundResource(R.drawable.play_icon);

                    try {
                        audioPath = audioRecorder.startRecording(oneDiary.getId().toString()+".mp4");
                    } catch (IOException e) {
                        Log.d("recorder","failed");
                        e.printStackTrace();
                    }
                    addAudioButton.setBackgroundResource(R.drawable.stop_record_icon);
                }
                isRecording = !isRecording;
            }
        });

        deleteAudioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                audioRecorder.stopRecording();
                audioPlayer.stop();
                if( audioPath!=null){
                    audioRecorder.deleteAudio();
                    audioPath = null;
                    Toast.makeText(getActivity().getApplicationContext(),"Audio deletado",Toast.LENGTH_SHORT).show();
                }
            }
        });

        deletePhotoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( !(bitmap==null) ){
                    bitmap = null;
                    imageView.setImageResource(R.drawable.ha_ha);
                }
            }
        });

        addDiaryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    saveNewDiary();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                returnToMainActivity();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( !(audioPath==null)){
                    audioRecorder.deleteAudio();
                }
                returnToMainActivity();
            }
        });



        datePickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
                DialogFragment fragment = DatePickerFragment.newInstance(new Date());
                fragment.setTargetFragment(NewDiaryFragment.this, REQUEST_DATE);
                fragment.show(fm,null);
            }
        });
        return v;
    }

    public void saveNewDiary() throws IOException {
        oneDiary.setAudioPath(audioPath);
        savePhoto();
        oneDiary.setText(textEditText.getText().toString());
        oneDiary.setDate(date);
        diaryDB.addDiary(oneDiary);
    }

    public void savePhoto() throws IOException {
        if(bitmap!=null){
            photoPath = oneDiary.getId().toString();
            FileOutputStream fos = getActivity().openFileOutput(photoPath, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
            photoPath = getActivity().getFilesDir() + "/" + photoPath;
            oneDiary.setPhotoPath(photoPath);
            //fos.flush();
            fos.close();
        }
    }

    public void returnToMainActivity(){
        Intent i = new Intent(getActivity(),DiaryList.class);
        startActivity(i);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == MyActivity.RESULT_OK) {
            Bundle extras = data.getExtras();
            bitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(Rounder.getRoundedShape(bitmap, 300, 300));
        }

        if (requestCode == REQUEST_DATE & resultCode == MyActivity.RESULT_OK) {
            date = (Date) data.getSerializableExtra(DatePickerFragment.Extra_Date);
            datePickerButton.setText(df[0].format(date));
        }
        if (requestCode == PICK_FROM_FILE & resultCode == MyActivity.RESULT_OK) {
            String path;
            mImageCaptureUri = data.getData(); //from Gallery
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = null;
            cursor  = getActivity().getContentResolver().query(
                    mImageCaptureUri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);
            cursor.close();
            bitmap = BitmapFactory.decodeFile(path);
            imageView.setImageBitmap(Rounder.getRoundedShape(bitmap, 300, 300));
        }
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        diaryDB.close();
        audioPlayer.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}

Logcat

    12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina D/AndroidRuntime: Shutting down VM
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d89b20)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime: FATAL EXCEPTION: main
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime: Process: com.here.superaitaotaotv.splentemedina, PID: 6934
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:73 flg=0x1 }} to activity {com.here.superaitaotaotv.splentemedina/com.here.superaitaotaotv.splentemedina.NewDiary}: java.lang.NullPointerException
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:  Caused by: java.lang.NullPointerException
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.here.superaitaotaotv.splentemedina.Rounder.getRoundedShape(Rounder.java:41)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.here.superaitaotaotv.splentemedina.NewDiaryFragment.onActivityResult(NewDiaryFragment.java:311)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.access$1300(ActivityThread.java:135) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
12-01 15:24:58.455 6934-6934/com.here.superaitaotaotv.splentemedina E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

IMAGE WITH THE GALLERY OPTIONS I MENTIONED ABOVE

  • Welcome to sopt. Click [Edit] and add the image directly on the site, this link didn’t even open here. Also add the class that captures in your app.

  • edited friend ;.

  • Doesn’t close but gives error? What error gives?

  • says the app stopped : "my diary stopped" however only error to pull the image, the app remains open

  • You need to review how you get the bitmap. Error says it is null when you pass it to the method getRoundedShape()

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.