Ошибка при передаче изображения экрана из приложения Android в Twitter в Kotlin - PullRequest
0 голосов
/ 16 февраля 2020

Что я хотел бы сделать

Я хотел бы поделиться изображением экрана из Android приложения в Twitter в Kotlin.

Проблема

Я могу подключитесь к странице входа в Twitter из приложения, даже если приложение Twitter не установлено.

Однако содержимое, которым я хочу поделиться из приложения, например текст и изображения, не было передано в Twitter.

Как я могу исправить свой текущий код в твите с изображением экрана в приложении?

Текущая программа

MainActivity.kt

package com.example.snsshare

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.SeekBar.OnSeekBarChangeListener
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.widget.Button
import androidx.core.app.ShareCompat
import androidx.core.content.FileProvider
import java.io.File


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //intent button
        val intentTweetButton: Button = findViewById(R.id.intentTweetButton)
        intentTweetButton.setOnClickListener {
            shareTwitter()
        }


        seekbar.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener {
                //called by 
                override fun onProgressChanged(
                    seekBar: SeekBar, progress: Int, fromUser: Boolean) {

                    background_view.setBackgroundColor(Color.rgb(progress,200,255 - progress))
                }

                override fun onStartTrackingTouch(seekBar: SeekBar) {
                    // called when the bar is touched 
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                    // called when the bar is released 
                }

            })

        share_button.setOnClickListener() {
            showShareChooser()
        }
    }

    //share intent
    fun shareTwitter() {
        val message = "shareTwitter intent tweet"
        try {
            val sharingIntent = Intent(Intent.ACTION_SEND)
            sharingIntent.setClassName("com.twitter.android", "com.twitter.android.PostActivity")
            sharingIntent.putExtra(Intent.EXTRA_TEXT, message)
            startActivity(sharingIntent)
        }
        catch (e: Exception) {
            Log.e("In Exception", "Comes here")
            val i = Intent()
            i.putExtra(Intent.EXTRA_TEXT, message)
            i.action = Intent.ACTION_VIEW
            i.data = Uri.parse("https://mobile.twitter.com/compose/tweet")
            startActivity(i)
        }

    }

    //share compat
    private fun showShareChooser() {

        val chooserTitle = “how to share”
        val subject = “title”
        val text = “name of the app”


        val builder = ShareCompat.IntentBuilder.from(this)
        builder.setChooserTitle(chooserTitle) // title to share
            .setSubject(subject) 
            .setText(text) // content text
            .setType("text/plain") 

        builder.startChooser()
    }


}

activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/background_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">

        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_margin="20dp"
            android:background="#ccc"
            android:max="255"/>

        <Button
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ShareCompat"/>

        <Button
            android:id="@+id/intentTweetButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="intent tweet" />


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Среда разработки

Android Studio 3.5.3

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...