Search code examples
androidonactivityresult

onActivityResult() is not called. I can't display a picture


I'm using Intent to call camera and gallery. I call camera or gallery, but onActivityResult() is not called. I can take a picture and can open gallery, but I can't display the picture. where should I fix? please teach me.

    public static class SelectImageDialog extends DialogFragment{
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
            builder.setMessage("please select").setPositiveButton("camera",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent=new Intent();
                    intent.setAction("android.media.action.IMAGE_CAPTURE");
                    startActivityForResult(intent,CAMERA);
                }
            }).setNegativeButton("gallery",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent=new Intent();
                    intent.setAction(intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent,GALLERY);
                }
            });
            return builder.create();
        }
    }

    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode==CATEGORY){
            if(resultCode==RESULT_OK) {
                Bundle bundle = data.getExtras();
                textCategory.setText(bundle.getString("item"));
            }
        }else if(requestCode==GALLERY){
            if(resultCode==RESULT_OK){
                try{
                    InputStream is=getContentResolver().openInputStream(data.getData());
                    Bitmap bitmap=BitmapFactory.decodeStream(is);
                    imageButton.setImageBitmap(bitmap);
                    is.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }else if(requestCode==CAMERA){
            if(resultCode==RESULT_OK){
                Bitmap bitmap=(Bitmap)data.getExtras().get("data");
                imageButton.setImageBitmap(savePic(bitmap));
            }
        }
    }

    public Bitmap savePic(Bitmap bitmap){
        String path= Environment.getExternalStorageDirectory().getPath();
        String fileName = System.currentTimeMillis() + ".png";
        try {
            FileOutputStream outputStream = new FileOutputStream(path+"/"+fileName);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        String[] paths = {Environment.getExternalStorageDirectory().getPath()+"/"+fileName};
        String[] mimeType = {"image/png"};
        MediaScannerConnection.scanFile(getApplicationContext(), paths, mimeType, new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String s, Uri uri) {
            }
        });
        return bitmap;
    }

Solution

  • this is how i am calling camera

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File tempFile = File.createTempFile("my_app", ".jpg");
    fileName = tempFile.getAbsolutePath(); 
    Uri uri = Uri.fromFile(fileName );
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri );
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);