Android 分享单个文件

本文介绍了Android平台上的分享功能实现方式,包括如何分享文本、单个文件等,并提供了具体代码示例。此外,还展示了如何通过Intent调用自己的Activity来实现定制化的分享流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近了解了一点关于Android 分享的东西 .

 有 图片 ,字符串分享,我这里简单的汇总一下 ,希望可以在忘记的时间看一下.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Share Chooser"));

系统会自动调用手机上安装过的可以分享发送的APK,供选择.   


分享单个文件,eg test.txt   

File sendFile = new File(filePath + File.separator + FILENAME);
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sendFile));
share.setType("*/*");// 
startActivity(Intent.createChooser(share, "Share"));


最后,还有一个调用自己写的activity,进行数据发送的(这里不写自己activity),只写intent.choose  

File sendFile = new File(filePath + File.separator + FILENAME);
if (sendFile.exists()) {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction("android.intent.action.XXMM");  
intent.setDataAndType(Uri.parse(filePath + "test.txt"),
"mark/nimei");


startActivity(Intent.createChooser(intent, "Share Chooser"));
} else {


Toast.makeText(getApplicationContext(), "请确认是否有这个文件?",
Toast.LENGTH_SHORT).show();
}

下面是一个manifest中的一部分 ,调用的时间,intent 过滤

     <!-- 下面的两个activity ,可以是自己写的  intent的 content , 通过intent 的方式 -->
        <activity
            android:name=".TestActivity"
            android:label="你妹啊" >
            <intent-filter>
                <action android:name="android.intent.action.XXMM" />


                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />


                <data android:mimeType="mark/nimei" />
            </intent-filter>
        </activity>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值