我无法从应用中分享我的图片以作为附件发送消息 我有一个可绘制图像的int数组列表
final int [] imagelistId={
R.drawable.birthday1,
R.drawable.birthday2,
R.drawable.birthday3,
R.drawable.birthday4,
R.drawable.birthday5,
R.drawable.birthday6,
};
之后,我将此代码作为附件共享
smsBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
// add the message at the sms_body extra field
smsIntent.setData(Uri.parse("mmsto:"));
smsIntent.setType("image/*");
smsIntent.putExtra("sms_body", "Image");
smsIntent.putExtra("subject", "Image Message");
smsIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.example.finalgreetings/" + imagelistId[pos]) );
try{
mcontext.startActivity(smsIntent);
} catch (Exception ex) {
Toast.makeText(mcontext, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
我看过很多堆栈溢出问题,但没有人回答我的问题。 我还读过关于将drawable转换为位图并保存到内部或外部存储然后共享但不知道如何操作的内容。 请建议我最好和最简单的解决方案。提前完成。
答案 0 :(得分:0)
试试这个...... !!!
smsBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);//put here your image id
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/LatestShare.png";
OutputStream out = null;
File file = new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
Intent shareIntent = new Intent();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share with"));
}
});