gpt4 book ai didi

java - 如何在 Kotlin 中从 Android 应用程序发送文本和图像?

转载 作者:行者123 更新时间:2023-12-01 19:28:53 26 4
gpt4 key购买 nike

我想做什么

我想从 Android 应用程序发送推文文本和图像。

我能够在应用程序的屏幕上放置要发推文的图像,并且该应用程序可以通过按钮打开推文表单。

作为引用,我检查了以下前一个问题。

How to Share Image + Text together using ACTION_SEND in android?

问题

问题是程序上设置的文本和图像数据未出现在 Twitter 上。

它只显示空白推文表单。

如何将这些文本和图像从 Android 应用移至推文表单?

代码

MainActivity.kt

package com.example.tweets

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.core.app.ShareCompat
import java.io.File
import android.R.attr.path
import android.graphics.BitmapFactory
import kotlinx.android.synthetic.main.activity_main.*
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import java.io.IOException


class MainActivity : AppCompatActivity() {

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

try {resources.assets.open("sample.jpg").use { istream ->
val bitmap = BitmapFactory.decodeStream(istream)
sample.setImageBitmap(bitmap)
}
} catch (e: IOException) {
e.printStackTrace()
}


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

/*

val shareCompatButton: Button = findViewById(R.id.shareCompatButton)
shareCompatButton.setOnClickListener {
shareCompat()
}
*/
}

fun shareTwitter() {
val message = "shareTwitter intent tweet"
val bmpUri = Uri.parse("file://$path")
try {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.setClassName("com.twitter.android", "com.twitter.android.PostActivity")
val imageUri = Uri.parse("sample.jpg")
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
sharingIntent.type = "image/jpeg"
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
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)
}

}

/*

fun shareCompat() {
val message = "shareCompat"
val builder = ShareCompat.IntentBuilder.from(this)
builder.setChooserTitle("Choose App")
builder.setText(message)
builder.setType("text/plain")
builder.addStream(Uri.fromFile(File("sample.jpg")))
builder.setType("image/jpg")
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">

<ImageView
android:id="@+id/sample"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/img_description"
tools:ignore="InvalidId" />

<Button
android:id="@+id/intentTweetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="intent tweet"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<!--
<Button
android:id="@+id/shareCompatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="ShareCompat"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/intentTweetButton" />
-->

</androidx.constraintlayout.widget.ConstraintLayout>

我尝试做什么

我尝试使用share compat而不是intent,但它也不起作用。

除此之外,share compat 仅适用于在手机上下载 Twitter 应用程序的用户,因此我更喜欢使用 intent 来发布来自 Twitter 的文本和图像Android 应用程序。

图像位于app/assets/sample.jpg

环境

Android Studio 3.5.3

Kotlin 插件 1.3.50

最佳答案

试试这个

 private void shareTwitter(String message) {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");

PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);

boolean resolved = false;
for (ResolveInfo resolveInfo : resolvedInfoList) {
if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
resolved = true;
break;
}
}
if (resolved) {
startActivity(tweetIntent);
} else {
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
startActivity(i);
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
}

private String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 should always be supported", e);
return "";
}
}

关于java - 如何在 Kotlin 中从 Android 应用程序发送文本和图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60413993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com