亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

android中activity可以響應外部的action的例子(可以用這個來導入外部文件)

標簽:
Android

效果:

https://img1.sycdn.imooc.com//5c1b586d0001281506401106.jpg

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

<activity
    android:name=".export.ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.EDIT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:mimeType="application/pdf" />
        <data android:mimeType="image/bmp" />
        <data android:mimeType="image/tiff" />
        <data android:mimeType="image/gif" />
        <data android:mimeType="image/jpeg" />
        <data android:mimeType="image/x-ms-bmp" />
        <data android:mimeType="image/png" />
        <data android:mimeType="image/x-pcx" />
        <data android:mimeType="image/targa" />
        <data android:mimeType="image/x-photoshop" />
        <data android:mimeType="text/plain" />
        <data android:mimeType="text/xml" />
        <data android:mimeType="text/html" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

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

        <data
            android:host="*"
            android:mimeType="*/*"
            android:pathPattern=".*\\.pdf"
            android:scheme="file" />

        <data
            android:host="*"
            android:mimeType="*/*"
            android:pathPattern=".*\\.jpg"
            android:scheme="file" />

        <data
            android:host="*"
            android:mimeType="*/*"
            android:pathPattern=".*\\.bmp"
            android:scheme="file" />
        <data
            android:host="*"
            android:mimeType="*/*"
            android:pathPattern=".*\\.png"
            android:scheme="file" />

        <data
            android:host="*"
            android:mimeType="*/*"
            android:pathPattern=".*\\.txt"
            android:scheme="file" />

    </intent-filter>
</activity>

然后在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的文件夹,把这个文件拷贝进去:

代码在:https://github.com/nickgao1986/StepSport

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消