添加图片到twitter分享意图android

问题描述 投票:9回答:3

我正在尝试将图像添加到我的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中没有异常我的应用程序只显示一个吐司说图像无法加载。

请问我做错了什么?

android android-intent twitter android-file
3个回答
11
投票

这就是你需要的

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

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();
    }
}

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.