我正在为此应用程序集成分享功能,使用以下代码:
private void socialShare()
{
Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp");
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share from"));
}
如上代码所示,我正在尝试放置drawable文件夹中的png图像
,但是该图像无法发送。这是因为setType中使用了image/jpeg吗? 我不能使用jpeg,因为它会失去透明度。能否有人建议我如何分享图片?
以下是我用来将图像从drawable复制到sdcard的代码:
String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder";
File direct = new File(commonPath);
if(!direct.exists())
{
if(direct.mkdir())
{
Log.d("tag","directory created");
//directory is created;
}
}
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.sharingimage);
OutputStream outStream = null;
File savingFile = new File(commonPath, "shareImage.png");
if(!savingFile.exists())
{
Log.d("tag","file is created");
try {
savingFile.createNewFile();
outStream = new FileOutputStream(savingFile);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Log.d("tag","Saved");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}