Android file transfer/Upload
一. 若想在Android上利用系统已有的Intent 发送/上传文件,可以尝试如下:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(intent);
系统会弹出可供选择的Intent。
如果想要指定的Intent,那么再设置component参数即可:
setComponent(new Component("com.android.***/*Packagename*/"),("com.android.***.###/*full class name*/"))
如果想发生/上传多个文件,那么请使用ACTION_SEND_MULTIPLE
对应的Extra设置方法,需要调整为put*ArrayListExtra,其它依样画葫芦就可以了。
二. 格外补充:
如果希望自行开发的Intent支持类似的功能,那么需要在自己的功能注册进系统中。
在Menifest中的相应Activity中加入类似代码。
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="yourapp/yourtype" />
</intent-filter>
别人就是使用如下代码调用了:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("yourapp/yourtype");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(intent);