我想在android.my代码中指定路径共享图像是工作但图像不像Facebook,谷歌驱动器等分享..!,我的分享意图不发送图像到其他应用程序! 什么是错误的代码帮助我任何人非常感谢!
这是我的代码:
File wallpaperDirectory = new File("/sdcard/myimage/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
File file = new File(wallpaperDirectory, "Share.jpg");
try {
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(),5000).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(file.getAbsolutePath());
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
这是myoutput:
单击选项后
不会将图像共享给其他应用程序!
答案 0 :(得分:0)
我在链接下面找到了很好的图像共享教程:
http://collegewires.com/android/2012/06/android-implementing-a-share-intent/
我的问题是文件路径不正确在file://
之前添加路径
如下代码:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file://" + file.getAbsolutePath());
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
现在我的问题解决了!
答案 1 :(得分:0)
如果您已经在bitmap
变量中加载了位图,请尝试以下方法:
Uri U = null;
File folderShare = new File(btnShare.getContext().getExternalFilesDir(null) + "/download/");
folderShare.mkdirs();
File fileShare = new File(folderShare, "Image-android.jpg");
fileShare.delete();
fileShare = new File(folderShare, "Image-android.jpg");
if (fileShare.isFile())
try {
FileOutputStream out = new FileOutputStream(fileShare);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
U = Uri.fromFile(fileShare);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
String email_subject = getString(R.string.email_subject);
String email_content = getString(R.string.email_content);
String email_chooser = getString(R.string.email_chooser);
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, email_subject);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, U);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, email_content);
v.getContext().startActivity(Intent.createChooser(emailIntent, email_chooser));