1

Currently I create an app which has the camera function that allows users to select their image or do capturing.I get the tutorial from https://stackoverflow.com/a/22165449/5261462. But I want the selected image intent to another page instead of just displaying on imageView. The image need to fix the screen and can add caption at below like whatsapp.

This is what I've tried so far.

Everything start from Project1.java, with the imagebutton.

 public void addListenerOnButton() {

        imageButton = (ImageButton) findViewById(R.id.imageButton);

        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ImageFitScreen i = new ImageFitScreen();
                i.selectImage();
            }

        });

    }

ImageFitScreen.java

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.image_fit_screen);

            b = (ImageView) findViewById(R.id.imageView3);
            t = (EditText) findViewById(R.id.editText38);
            u = (EditText) findViewById(R.id.editText39);
        }

        public void selectImage() {

            final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
            AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);
            builder.setTitle("Add Photo!");
            builder.setItems(options, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (options[item].equals("Take Photo"))
                    {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                       File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                       startActivityForResult(intent, 1);
   }
                     else if (options[item].equals("Choose from Gallery"))
                      {
                        Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                       startActivityForResult(intent, 2);
                       }
                       else if (options[item].equals("Cancel")) {
                       dialog.dismiss();
                       }
                       }
                         });

            builder.show();

        }

image_fit_screen

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="574dp"
        android:layout_height="523dp"
        android:id="@+id/imageView3"
        android:layout_x="6dp"
        android:layout_y="0dp" />

    <EditText
        android:layout_width="388dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText38"
        android:layout_x="8dp"
        android:layout_y="435dp" />

    <EditText
        android:layout_width="386dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText39"
        android:hint="Add a caption"
        android:layout_x="2dp"
        android:layout_y="410dp" />

</AbsoluteLayout>

But I get error as below when the imageButton in Project1.java is clicked.

11-03 11:44:44.800 31219-31219/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.project.project, PID: 31219 java.lang.NullPointerException at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103) at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:108) at android.support.v7.app.AlertDialog$Builder.(AlertDialog.java:269) at com.example.project.project.ImageFitScreen.selectImage(ImageFitScreen.java:77) at com.example.project.project.Project1$2.onClick(Project1.java:80) at android.view.View.performClick(View.java:4654) at android.view.View$PerformClick.run(View.java:19438) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146)

(ImageFitScreen.java:77)

AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);

(Project1.java:80)

i.selectImage();

I am seriously in dire need of some advice. Can someone please please assist me with some advice. PLEASE : )?

Community
  • 1
  • 1
Hoo
  • 1,806
  • 7
  • 33
  • 66

1 Answers1

0

From what I understood, ImageFitScreen is an activity and should be started using an Intent i.e.

Intent i = new Intent(Project1.this,ImageFitScreen.class);
startActivity(i);

If you look at the exception, it tells you that context is null ie. ImageFitScreen.this is null at the line

AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);

This is because, activity will only have a context if it is started by the activitymanager. We use intent to ask activitymanger to start an activity. Hope it helps you.

UPDATE:

You can use ImageFitScreen to hold imageview and edittext for caption. Then start ImageFitScreeen when you need the user to pick an image. And onCreate of ImageFitScreen you can start selectImage() function ie.

Project1.java

public void addListenerOnButton() {

    imageButton = (ImageButton) findViewById(R.id.imageButton);

    imageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Project1.this,ImageFitScreen.class);
            startActivity(i);
        }

    });

}

ImageFitScreen.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_fit_screen);

        b = (ImageView) findViewById(R.id.imageView3);
        t = (EditText) findViewById(R.id.editText38);
        u = (EditText) findViewById(R.id.editText39);

        selectImage();
    }

    public void selectImage() {

        final CharSequence[] options = { "Take Photo", "Choose from             Gallery","Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo"))
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                   File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                   intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                   startActivityForResult(intent, 1);
                  }
                 else if (options[item].equals("Choose from Gallery"))
                  {
                    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                   startActivityForResult(intent, 2);
                   }
                   else if (options[item].equals("Cancel")) {
                   dialog.dismiss();
                   finish();
                   }
                   }
                     });
        builder.setOnKeyListener(new Dialog.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode,
                KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                finish();
                dialog.dismiss();
            }
            return true;
        }
    });
        builder.show();

    }
Fabin Paul
  • 1,701
  • 1
  • 16
  • 18
  • But how about if I want to do something like whatsapp? It will allow us to pick image from gallery. Once the image get selected, it will show the selected image in full screen and can add caption – Hoo Nov 03 '15 at 04:13
  • Yes bro, is what I looking for. But when I click the `cancel` in the dialog box, it shows `ImageFitScreen` instead of `Project1.java` – Hoo Nov 03 '15 at 04:31
  • 1
    bro, you can always use finish() to finish an activity. :) I have edited my answer... – Fabin Paul Nov 03 '15 at 04:33
  • how about use the return button that provided by the emulator? Do I need to add `public void onBackPressed()`? – Hoo Nov 03 '15 at 04:39
  • bro, I have edited my answer. This is one of the options. But I think user can still cancel/dismiss alertdialog by click outside it. Another solution is disabling this by added the code `builder.setCancelable(false);` – Fabin Paul Nov 03 '15 at 04:49
  • or you can use `builder.setCanceledOnTouchOutside(false);` – Fabin Paul Nov 03 '15 at 05:04
  • Thanks bro, it works like charm :) Why the captured image always shows landscape? – Hoo Nov 03 '15 at 05:24
  • I guess that is device specific and also depends on the orientation with which you captured the image. Try a different test device.. You can also refer this [link](http://stackoverflow.com/questions/17083366/how-to-set-captured-images-in-portrait-mode). Hope it helps you.. – Fabin Paul Nov 03 '15 at 05:31
  • If the user already in gallery and he wishes to return, how can it straight away back to `Project1.java` instead of `ImageFitScreen` – Hoo Nov 03 '15 at 16:27
  • In that case, onActivityResult resultCode will be RESULT_CANCELED. In such case you can simple finish the activity. – Fabin Paul Nov 03 '15 at 16:30
  • Did you mean onActivityResult in ImageFitScreen? – Hoo Nov 03 '15 at 16:34