写在20110903:ACTION_GET_CONTENT

让FM支持通过发送ACTION_GET_CONTENT获取文件
本文介绍如何在Android应用中实现通过发送ACTION_GET_CONTENT系统接口,利用其他具备浏览选择文件功能的应用,如图库、音乐播放器等,来支持添加附件功能。通过在AndroidManifest.xml中添加特定配置,当点击添加附件时,应用能够弹出选择框,让用户从多种文件源中选择文件,然后将选中的文件作为附件插入到应用中。
如彩信、Email、Gmail、网站等添加附件,如果应用本身没有做文件浏览选择的功能,此时应用可以通过发送ACTION_GET_CONTENT的系统接口来获取其他具备浏览选择文件功能的应用来支持该功能,比如此时添加附件会弹出一个选择框,有图库、音乐播放器等,现在如果想让FM也支持,在AndroidMenifest.xml中添加如下配置:
<intent-filter>
<action andriod:name="android.intent.action.GET_CONTENT"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimetype="*/*"/>
</intent-filter>

在点击Item打开File的时候添加如下代码:
String action=mContext.getIntent().getAction();
if(!TextUtils.isEmpty(action)&&action.equals(Intent.ACTION_GET_CONTENT)){
Uri uri=getDbUriFromFilepath(mContext,filepath);
//从数据库中查询该物理路径对应的Uri
if(uri!=null){
intent.setDataAndType(uri,type);
}else{
intent.setDataAndType(Uri.fromFile(new File(filepath)),type);
}
mContext.setResult(Activity.RESULT_OK,intent);
mContext.finish();
return;
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.tplink.design.card.TPConstraintCardView android:id="@+id/card_off_client" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" > <ImageView android:id="@+id/iv_client" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_game_console" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_14" android:layout_marginBottom="@dimen/tpds_all_dp_14" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <TextView android:id="@+id/tv_client_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/client_example_name" android:textSize="@dimen/tpds_all_text_size_17" android:layout_marginStart="@dimen/tpds_all_dp_16" app:layout_constraintStart_toEndOf="@id/iv_client" app:layout_constraintTop_toTopOf="@id/iv_client" /> <TextView android:id="@+id/tv_last_online_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/last_online_time_example" android:textColor="@color/grey_text" android:textSize="@dimen/tpds_all_text_size_14" app:layout_constraintStart_toStartOf="@id/tv_client_name" app:layout_constraintTop_toBottomOf="@id/tv_client_name"/> <ImageView android:id="@+id/iv_client_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_next" android:layout_marginEnd="@dimen/tpds_all_dp_12" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@+id/tv_client_name" app:layout_constraintBottom_toBottomOf="@+id/tv_client_name"/> <View android:id="@+id/divider_off_line" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray" android:layout_marginStart="@dimen/tpds_all_dp_72" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </com.tplink.design.card.TPConstraintCardView> <!-- 右侧操作按钮 --> <LinearLayout android:id="@+id/right_action_layout" android:layout_width="@dimen/tpds_all_dp_80" android:layout_height="0dp" android:background="@color/gray_bg" app:layout_constraintEnd_toEndOf="@id/card_off_client" app:layout_constraintTop_toTopOf="@id/card_off_client" app:layout_constraintBottom_toBottomOf="@id/card_off_client" > <!-- android:visibility="invisible"--> <ImageView android:id="@+id/iv_disable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_block_24" android:layout_marginTop="@dimen/tpds_all_dp_24" android:layout_marginStart="@dimen/tpds_all_dp_28" app:layout_constraintStart_toStartOf="@id/right_action_layout" app:layout_constraintEnd_toEndOf="@id/right_action_layout" app:layout_constraintTop_toTopOf="@id/right_action_layout" app:layout_constraintBottom_toBottomOf="@id/right_action_layout" /> </LinearLayout> <LinearLayout android:id="@+id/right_action_layout2" android:layout_width="@dimen/tpds_all_dp_80" android:layout_height="0dp" android:background="@color/red_bg" app:layout_constraintEnd_toStartOf="@id/right_action_layout" app:layout_constraintTop_toTopOf="@id/card_off_client" app:layout_constraintBottom_toBottomOf="@id/card_off_client" > <!-- android:visibility="invisible"--> <ImageView android:id="@+id/iv_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_delete_24" android:layout_marginTop="@dimen/tpds_all_dp_24" android:layout_marginStart="@dimen/tpds_all_dp_28" app:layout_constraintStart_toStartOf="@id/right_action_layout" app:layout_constraintEnd_toEndOf="@id/right_action_layout" app:layout_constraintTop_toTopOf="@id/right_action_layout" app:layout_constraintBottom_toBottomOf="@id/right_action_layout" /> </LinearLayout>如何实现后面两个linerlayout的滑动显示呢
最新发布
09-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值