0

我有以下代码有一个应用程序,它要求一个电子邮件地址、一条消息、一个选择图像的按钮和一个图像按钮,只有当应用程序获得一个电子邮件地址、一条消息和一个选择的文件时,它才应该可见。选择的文件应该加载到 imageButton 中。

一旦你按下 imageButton,它现在会显示选择的图像并且是可见的,应用程序应该打开电子邮件应用程序,填写收件人地址、邮件正文并附加选择的图像。

我有以下代码,但我无法将图像传递给电子邮件应用程序。

此外,我无法验证是否已加载图像以使 imageButton 可见。

import java.io.FileNotFoundException;
import java.io.InputStream;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener, TextWatcher {

    private static final int REQUEST_CODE = 1;
    EditText message;
    EditText address;
    Button buttonAttach;
    ImageButton buttonSend;
    String mAdress;
    String mMessage;
    private InputStream stream;
    Bitmap mBitmap;
    private ImageView v;
    private Uri uri;
    private boolean image;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        message = (EditText) findViewById(R.id.emailMessage);
        address = (EditText) findViewById(R.id.to_address);
        buttonAttach = (Button) findViewById(R.id.choose_button);
        buttonSend = (ImageButton) findViewById(R.id.imageButton);

        buttonAttach.setOnClickListener(this);
        buttonSend.setOnClickListener(this);

        message.addTextChangedListener(this);

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        //if buttonAttach is pushed, call openGallery method
        if (view == buttonAttach){
            openGallery();
        }
        //if buttonSend is pushed, try to get address, message and image
        if (view == buttonSend){
            try{
                String mAddress = address.getText().toString();
                String mMessage = message.getText().toString();

                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mAddress, null));

                i.putExtra(Intent.EXTRA_TEXT, mMessage);
                i.putExtra(Intent.EXTRA_STREAM, uri);

                startActivity(i);
            }
            //if exception is caught, display Toast.
            catch(Throwable th){
                Toast.makeText(this,"Could not send message: " + th.toString(), Toast.LENGTH_LONG).show();

            }
        }

    }

    // openGallery method
    private void openGallery() {
        // TODO Auto-generated method stub
        //Intent to provide a chooser to choose a picture.
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "Choose app..."), REQUEST_CODE);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        image = false;

        //if requestCode and resultCode are okay, get data from Intent data
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            Uri uri = data.getData();
            // Display Toast stating your choice of file.
            Toast.makeText(getApplicationContext(), "You chose the file: " + uri.toString(), Toast.LENGTH_LONG).show();

            //Try to get the stream and set the image.
            try {

                stream = getContentResolver().openInputStream(uri);
                mBitmap = BitmapFactory.decodeStream(stream);
                v = (ImageView) findViewById(R.id.imageButton);
                v.setImageBitmap(mBitmap);

                //if file not found, display Toast saying it could not open file.
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Log.e("OnActivityResult", "Could not open file.");
                Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show();
            }

        }



        image = true;
        super.onActivityResult(requestCode, resultCode, data);
    }




    @Override
    // After changing text on address or message make image visible.
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

        String text = message.getText().toString();
        String email = address.getText().toString();
        Log.i("afterTextChanged", "Email is:" + email);
        Log.i("afterTextChanged", "String text is:" + text);
        Animation anim = null;
        View view = findViewById(R.id.imageButton);
        boolean valid = text.length() > 0 && email.length() > 0;
        boolean isVisible = view.getVisibility() == View.VISIBLE;

        if (isVisible == valid){
            return;
            }

        if (valid){
            view.setVisibility(View.VISIBLE);
            anim = AnimationUtils.makeInAnimation(this, true);
        }else{
            view.setVisibility(View.GONE);
            anim = AnimationUtils.makeOutAnimation(this, true);
            }
        Log.i("Anim", "Animation starting.");
        view.startAnimation(anim);

    }




    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }




    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }
}

任何帮助,将不胜感激。

4

1 回答 1

1

现在在您的代码中,您永远不会为uriMainActivity 变量分配任何内容

因为 inonActivityResult Uri uri = data.getData();是局部变量,所以 MainActivity.uri 始终为空

于 2014-02-07T23:11:13.663 回答