我有疑问。
如何从使用ImageView功能的数组共享图像?
我的阵列有超过100张图片,例如:
Final int [] photos = {
R.drawable.abrir_a_boca,
R.drawable.rooms,
R.drawable.firmly,
R.drawable.agradeca,
R.drawable.alfaiate,
R.drawable.ancora,
}
分享我正在尝试使用Intent.ACTION_SEND
Set.setOnClickListener (new View.OnClickListener () {
@Override
Public void onClick (View v)
{
Intent sharingIntent = new Intent (Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse (photos???);
SharingIntent.setType ("image / *");
SharingIntent.putExtra (Intent.EXTRA_STREAM, screenshotUri);
StartActivity (Intent.createChooser (sharingIntent, "Share image using"));
}
});
如何分享图像?
非常感谢!!!
答案 0 :(得分:2)
在分享图片之前,您必须执行一些操作,因此请在Manifest.xml
中添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
拳头,从您的可绘制资源创建一个Bitmap
对象:
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx);
然后,获取您分享图像的路径
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/yourImage.jpg";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri uri = Uri.parse("file://"+path);
最后,创建你的意图:
Intent intent = new Intent();
intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpg");
startActivity(Intent.createChooser(intent,"Share with..."));