当我按下“共享”按钮时,短信正文可以正常显示,但我就是无法显示图像。只是看起来根本就没有任何附件。
我检查了所有代码,看起来不错,但我还不能说我是开发专家,所以我可能正在检查一些东西。
有人知道会发生什么吗?
<小时/>从数据库获取 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;
}
最佳答案
我通过将 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 不起作用,因为那只是文本。
另一件可能对某人有帮助的事情是,当我更改为 ACTION_SEND 时,它首先只放入图像,没有正文。我也解决了这个问题。
而不是这一行:
intentShare.putExtra("sms_body", "The sms body goes here";
替换为:
intentShare.putExtra(Intent.EXTRA_TEXT, "The sms body goes here");
这样您就可以在彩信中发送图像和文本。
关于java - 即使使用有效的 URI,图像也不会附加到文本消息中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46735382/