选择图像后意图进入新页面

时间:2015-11-03 03:51:51

标签: java android camera android-camera whatsapp

目前我创建的应用程序具有相机功能,允许用户选择图像或进行捕捉。我从https://stackoverflow.com/a/22165449/5261462获取教程。但我想将所选图像意图转移到另一个页面而不是仅显示在imageView 上。图像需要修复屏幕,并且可以在下方添加标题,如 whatsapp。

这是我到目前为止所尝试过的。

一切都从 Project1.java 开始,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>

但是当点击 Project1.java 中的imageButton时,我会收到如下错误。

  

11-03 11:44:44.800 31219-31219 / com.example.project.project   E / AndroidRuntime:致命异常:主要       处理:com.example.project.project,PID:31219       显示java.lang.NullPointerException               在android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)               在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)               在android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:108)               在android.support.v7.app.AlertDialog $ Builder。(AlertDialog.java:269)               在com.example.project.project.ImageFitScreen.selectImage(ImageFitScreen.java:77)               在com.example.project.project.Project1 $ 2.onClick(Project1.java:80)               在android.view.View.performClick(View.java:4654)               在android.view.View $ PerformClick.run(View.java:19438)               在android.os.Handler.handleCallback(Handler.java:733)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:146)

(适用ImageFitScreen.java:77)

  

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

(适用Project1.java:80)

  

i.selectImage();

我非常需要一些建议。有人可以请帮我一些建议。请:))?

1 个答案:

答案 0 :(得分:0)

根据我的理解,ImageFitScreen是一项活动,应该使用Intent开始,即

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

如果查看异常,它会告诉您上下文为null,即。 ImageFitScreen.this

行为空
AlertDialog.Builder builder = new AlertDialog.Builder(ImageFitScreen.this);

这是因为,如果activity由活动管理器启动,则活动将只有一个上下文。我们使用intent来让activitymanger开始一项活动。希望它可以帮到你。

<强>更新

您可以使用ImageFitScreen保存imageview和edittext以获取标题。然后在需要用户选择图像时启动ImageFitScreeen。并且onCreate of ImageFitScreen你可以启动selectImage()函数,即。

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();

    }