如何在消息android studio中发送图像?

时间:2020-07-01 08:49:00

标签: java image android-studio mms android-mms

我需要知道用于发送带有短信的图像的代码吗?我现在需要在代码中添加什么内容以发送彩信图像以及我正在使用android studio语言的文本是Java,它是一个短信调度程序 xml图片 xml for sms send

java代码 公共类NewSMSActivity扩展了AppCompatActivity,实现了galleryimage {

public static final int IMAGE_GALLERY_REQUEST = 20;
private ImageView imgPicture;
private EditText telephoneNo;
private EditText txtMessage;
private String phoneNo;
private String message;
BroadcastReceiver sendBroadcastReceiver = new SentReceiver();
BroadcastReceiver deliveryBroadcastReceiver = new DeliverReceiver();


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


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    init();
}


private void init() {
    Button sendBtn = findViewById(R.id.buttonSendMessage);
    telephoneNo = findViewById(R.id.editText);
    txtMessage = findViewById(R.id.editText2);
    ImageButton contact = findViewById(R.id.contact);



    contact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, Constants.RESULT_PICK_CONTACT);
        }
    });
    sendBtn.setOnClickListener(this);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.buttonSendMessage) {
        phoneNo = telephoneNo.getText().toString();
        message = txtMessage.getText().toString();

        if (phoneNo != null && phoneNo.trim().length() > 0) {

            if (message != null && message.trim().length() > 0) {

                sendSMSNow();

            } else
                txtMessage.setError(getString(R.string.please_write_message));

        } else
            telephoneNo.setError(getString(R.string.please_write_number));
    }
}

@Override
protected void onPause() {
    super.onPause();

    try {
        unregisterReceiver(sendBroadcastReceiver);
        unregisterReceiver(deliveryBroadcastReceiver);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void sendSMSNow() {

    String SENT = "SMS Sent";
    String DELIVERED = "SMS Delivered";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
    registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNo, null, message, sentPI, deliveredPI);

}

public void pickContact(View v) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, Constants.RESULT_PICK_CONTACT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == Constants.RESULT_PICK_CONTACT)
            contactPicked(data);
    } else {
        Log.e("MainActivity", "Failed");
    }
}

private void contactPicked(Intent data) {

    Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;

        Uri uri = data.getData();

        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();

        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        telephoneNo.setText(phoneNo);


    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void onImageGalleryClicked(View v) {
    // invoke the image gallery using an implict intent.
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

    // where do we want to find the data?
    File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String pictureDirectoryPath = pictureDirectory.getPath();
    // finally, get a URI representation
    Uri data = Uri.parse(pictureDirectoryPath);

    // set the data and type.  Get all image types.
    photoPickerIntent.setDataAndType(data, "image/*");

    // we will invoke this activity, and get something back from it.
    startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
}

@Override
public void OnActivityResult(int requestCode, int resultCode, Intent data) throws FileNotFoundException {
    if (resultCode == RESULT_OK) {
        if (requestCode == IMAGE_GALLERY_REQUEST) {


            Uri imageUri = data.getData();

            InputStream inputStream;
            try {
                inputStream = getContentResolver().openInputStream(imageUri);
                Bitmap image = BitmapFactory.decodeStream(inputStream);
                imgPicture.setImageBitmap(image);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this,"Unable to open image",Toast.LENGTH_LONG).show();

            }
        }
    }
}

}

0 个答案:

没有答案