質問

I am working on a share function in android, from the online tutorial I found the sample code and create the function like that. But it has some drawback(there are too many option if I installed a lot of app), so , in short, I would like to do the following:

  1. Strict the option to facebook twitter gmail
  2. How to share image (if I provide link for it? (twitter seems accept plain text only))
  3. The following code shows nothing in facebook app?(Can't share data on facebook)

Here is my code:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                try {
                    String newsUrl = Html.fromHtml( URLDecoder.decode(content[0], "UTF-8")).toString().replace("appfunc://share=", "");
                    String title = Html.fromHtml( URLDecoder.decode(content[1], "UTF-8")).toString();
                    String newsContent = Html.fromHtml( URLDecoder.decode(content[2], "UTF-8")).toString();
                    if (!newsContent.equals(""))
                        newsContent += "...\n\n";
                    sendIntent.putExtra(Intent.EXTRA_TEXT, title + "\n\n" + newsContent + ctx.getResources().getString(R.string.link) + ": " + newsUrl);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    sendIntent.putExtra(Intent.EXTRA_TEXT, ctx.getResources().getString(R.string.share_placeholder));
                    e.printStackTrace();
                };
                ctx.startActivity(Intent.createChooser(sendIntent, ctx.getResources().getString(R.string.share_to)));
役に立ちましたか?

解決

You can share images using shareIntent like this

private Intent createShareIntent()
 {
    shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+CreateTempFile(bitmapImage)));
    return shareIntent;
}
private File CreateTempFile(Bitmap myBitmap)
{

     try 
     {
    File SharingFile = File.createTempFile("OriginalImage", ".jpeg",temporaryFile);
        FileOutputStream out = new FileOutputStream(SharingFile);
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

    } 
     catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


return SharingFile;

}

Doing something like this will open all apps that will accept your image.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top