即使使用有效的 URI,图像也不会附加到文本消息中

Image wont attach to a text message even with a working URI

当我按下“分享”按钮时,SMS 正文进入正常,但我就是无法显示图像。就是看起来连附件都没有。

我检查了我所有的代码,看起来不错,但我还不能说我是开发方面的专家,所以我可能正在检查一些东西。

有人知道会发生什么吗?


从数据库获取 URI(我知道 URI 是正确的,因为 imageview 基于相同的 URI 正确显示):

imageURI = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE)));

这是我尝试将 URI 设置为附件的地方:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   int id = item.getItemId();

if (id == R.id.action_shareWine) {
Intent intentShare = new Intent(Intent.ACTION_SENDTO);
        intentShare.setData(Uri.parse("smsto:"));  // This ensures only SMS apps respond
        intentShare.putExtra("sms_body", "The sms body goes here";
//Attaching the image I want into the text:
        intentShare.putExtra(intentShare.EXTRA_STREAM, imageURI);
        if (intentShare.resolveActivity(getPackageManager()) != null) {
            startActivity(intentShare);
        }

如果有帮助,这就是我最初获取 URI 的方式:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();

        } catch (IOException ex) {

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            photoURI = FileProvider.getUriForFile(this,
                    "jeremy.com.wineofmine.fileprovider",
                    photoFile);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

在调用startActivity()之前先调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)ACTION_SENDIntent。现在,您正在附加 Uri,但收件人无权阅读 Uri 标识的内容。

我通过将 intentShare 代码更改为此来修复它:

Intent intentShare = new Intent(Intent.ACTION_SEND);
        intentShare.putExtra(intentShare.EXTRA_STREAM, imageURI);
        intentShare.setType("image/*");
        intentShare.putExtra(Intent.EXTRA_TEXT, "The sms body goes here");
        if (intentShare.resolveActivity(getPackageManager()) != null) {
            startActivity(intentShare);
        }

ModularSynth 帮助我意识到 ACTION_SENDTO 行不通,因为那只是文本。

另一件可能对某人有帮助的事情是,当我更改为 ti ACTION_SEND 时,它一开始只是放入图像,没有主体。我也修好了。

代替这一行:

intentShare.putExtra("sms_body", "The sms body goes here";

替换为:

 intentShare.putExtra(Intent.EXTRA_TEXT, "The sms body goes here");

这样您就可以在彩信中同时发送图片和文字。