我正在开发一个Android应用程序,我需要在按钮点击时共享图像。但我只获得图像URl。那么,我该如何分享图片???
如果我给意图提供图片网址,我会得到空的附件。
我的代码是:
sharebut =(Button)findViewById(R.id.sharebut);
sharebut.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
String screenshotUri = flag;
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
答案 0 :(得分:0)
在Uri.parse("file:///"+ yourImagePath)
的sd卡中添加图片所在的路径
使用:
String path= "/Downloads/image1.jpg"; //Add your path here
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+ path));
答案 1 :(得分:0)
请通过来自网址的电子邮件尝试此解决方案以获取共享图片。
String path = "";
URL url;
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, description);
try {
url = new URL(thumnbnailURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap immutableBpm = BitmapFactory.decodeStream(input);
Bitmap mutableBitmap = immutableBpm.copy(
Bitmap.Config.ARGB_8888, true);
View view = new View(VideoDetailsActivity.this);
view.draw(new Canvas(mutableBitmap));
path = Images.Media.insertImage(getContentResolver(),
mutableBitmap, "Nur", null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(path);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
答案 2 :(得分:0)
我找到了解决方案.. :) 刚刚创建了一个文件并在imageview中共享内容。
sharebut =(Button)findViewById(R.id.sharebut); sharebut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgflag.buildDrawingCache();
Bitmap bmap = imgflag.getDrawingCache();
OutputStream out = null;
String path =Environment.getExternalStorageDirectory().toString();
File file = new File(path, "test.png");
try {
file.createNewFile();
out = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Your Image"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});