android action格式,android中activity可以响应外部的action的例子(可以用这个来导入外部文件)...

效果:

AAffA0nNPuCLAAAAAElFTkSuQmCC

如果使自己的应用也出现在这个列表上,必须在menifest的这个activity下加入:

android:name=".export.ShareActivity">

android:host="*"

android:mimeType="*/*"

android:pathPattern=".*\\.pdf"

android:scheme="file" />

android:host="*"

android:mimeType="*/*"

android:pathPattern=".*\\.jpg"

android:scheme="file" />

android:host="*"

android:mimeType="*/*"

android:pathPattern=".*\\.bmp"

android:scheme="file" />

android:host="*"

android:mimeType="*/*"

android:pathPattern=".*\\.png"

android:scheme="file" />

android:host="*"

android:mimeType="*/*"

android:pathPattern=".*\\.txt"

android:scheme="file" />

然后在activity的oncreate上接收这个intent:@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Intent intent = this.getIntent();

int flags = intent.getFlags();

if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {

if (intent.getAction() != null && Intent.ACTION_VIEW.equals(intent.getAction())) {

if (SCHEME_FILE.equals(intent.getScheme()) || SCHEME_CONTENT.equals(intent.getScheme())) {

String i_type = getIntent().getType();

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

mImportingUri = intent.getData();

System.out.println("====mImportingUri=" + mImportingUri);

InputStream is = null;

try {

is = getContentResolver().openInputStream(mImportingUri);

} catch (Exception e) {

System.out.println("====e=" + e);

}

if (mImportingUri != null && SCHEME_FILE.equalsIgnoreCase(mImportingUri.getScheme())) {

//Is file

startToCopyFile(is);

} else if (mImportingUri != null && SCHEME_CONTENT.equalsIgnoreCase(mImportingUri.getScheme())) {

startCopyMedia(is);

}

}

}

}

}private boolean startToCopyFile(InputStream is) {

String fileName = getSDPath() + tmpPath + File.separator + getName(mImportingUri);

makesureFileExist(fileName);

File toFile = new File(fileName);

CopyThread mCopyThread = new CopyThread(is, toFile);

new Thread(mCopyThread).start();

return true;

}

private class CopyThread implements Runnable {

private File toFile;

private InputStream fosfrom = null;

public CopyThread(InputStream fosfrom, File toFile) {

this.fosfrom = fosfrom;

this.toFile = toFile;

}

@Override

public void run() {

try {

TimeUnit.MILLISECONDS.sleep(800);

FileInputStream fosfrom = null;

if (this.fosfrom != null) {

fosfrom = (FileInputStream) this.fosfrom;

}

FileOutputStream fosto = new FileOutputStream(toFile);

byte bt[] = new byte[1024];

int c;

int time = 0;

while ((c = fosfrom.read(bt)) > 0) {

fosto.write(bt, 0, c);

}

if (fosfrom != null) {

fosfrom.close();

}

fosto.close();

} catch (Exception e) {

return;

} finally {

try {

if (this.fosfrom != null) {

this.fosfrom.close();

}

} catch (IOException e) {

}

}

}

}

如果发现要导入的文件是txt格式,则我们在sd卡下建立一个tmp的文件夹,把这个文件拷贝进去:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值