添加图片到twitter分享意图android

时间:2013-10-01 15:20:42

标签: android android-intent twitter android-file

我正在尝试将图片添加到我的Twitter分享意图中。我在一个类中本地保存图像,然后在另一个类中保存图像并尝试附加到我的意图。

这是我的代码

private void shareTwitter(){

    try {

        FileInputStream fis;
        fis = getActivity().openFileInput("photo.jpg");
        Bitmap shot = BitmapFactory.decodeStream(fis);
        File file = new File(MapView.path, "snapshot.jpg");
        if(file.exists()){
            Log.i("FILE", "YES");
        }else{
            Log.i("FILE", "NO");
        }
        Uri uri = Uri.parse(file.getAbsolutePath());
        //Uri uri = Uri.parse("android.resource://com.gobaby.app/drawable/back");             
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("/*");
            intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
            intent.putExtra(Intent.EXTRA_TEXT, "Thiws is a share message");
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(intent);            

    } catch (final ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

目前我的logcat中没有异常,我的应用程序只显示一个吐司,说图像无法加载。

请问我做错了什么?

3 个答案:

答案 0 :(得分:11)

这就是你需要的

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);

答案 1 :(得分:7)

这对某些人可能有帮助:

private void sendShareTwit() {
    try {
        Intent tweetIntent = new Intent(Intent.ACTION_SEND);

        String filename = "twitter_image.jpg";
        File imageFile = new File(Environment.getExternalStorageDirectory(), filename);

        tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
        tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
        tweetIntent.setType("image/jpeg");
        PackageManager pm = getActivity().getPackageManager();
        List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
        boolean resolved = false;
        for (ResolveInfo ri : lract) {
            if (ri.activityInfo.name.contains("twitter")) {
                tweetIntent.setClassName(ri.activityInfo.packageName,
                        ri.activityInfo.name);
                resolved = true;
                break;
            }
        }

        startActivity(resolved ?
                tweetIntent :
                Intent.createChooser(tweetIntent, "Choose one"));
    } catch (final ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
    }
}

答案 2 :(得分:0)

这是解决方法:

private fun shareOnTwitter() {
    val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
    val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)

    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "image/jpeg"
        putExtra(Intent.EXTRA_STREAM, uriForFile)
    }
    startActivity(intent)
}