首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android透过ContentProvider传输文件

2012-09-08 
Android通过ContentProvider传输文件我们知道Android两个应用程序之间进行数据交互需要通过ContentProvide

Android通过ContentProvider传输文件

我们知道Android两个应用程序之间进行数据交互需要通过ContentProvider,而且通常都是数据库的操作。
今天项目需要使用Android的ContentProvider交互普通SD卡上的文件,于是我写了这个小例子:
AndroidManifest.xml

package com.h3c.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import android.app.Activity;import android.content.Context;import android.content.pm.PackageManager.NameNotFoundException;import android.content.res.AssetFileDescriptor;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotepadTestActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.notepad);        Button button = (Button) findViewById(R.id.notepad);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                try {                    // 直接读文件                    // InputStream is = getContentResolver().openInputStream(                    // Uri.parse("file:/mnt/sdcard/h3c.txt"));                    //                    // File bkFile = new File("/mnt/sdcard/h3c2.txt");                    // if (!bkFile.exists()) {                    // bkFile.createNewFile();                    // }                    //                                        // FileOutputStream out = new FileOutputStream(bkFile);                    // byte[] b = new byte[1024 * 5]; // 5KB                    // int len;                    // while ((len = is.read(b)) != -1) {                    // out.write(b, 0, len);                    // }                    // out.flush();                    // is.close();                    // out.close();                    // 直接写文件                    // OutputStream out = getContentResolver().openOutputStream(                    // Uri.parse("file:/mnt/sdcard/h3c.txt"));                    // FileInputStream in = new FileInputStream(new File(                    // "/mnt/sdcard/h3c3.txt"));                    //                    // byte[] b = new byte[1024 * 5]; // 5KB                    // int len;                    // while ((len = in.read(b)) != -1) {                    // out.write(b, 0, len);                    // }                    // out.flush();                    //                                        // in.close();                    // out.close();                    // 内容流写                    // AssetFileDescriptor afd = getContentResolver()                    // .openAssetFileDescriptor(                    // Uri.parse("content://com.h3c.test/h3c.txt"),                    // "wr");                    // InputStream in = afd.createInputStream();                    // File bkFile = new File("/mnt/sdcard/h3c2.txt");                    // if (!bkFile.exists()) {                    // bkFile.createNewFile();                    // }                    //                    // FileOutputStream out = new FileOutputStream(bkFile);                    // byte[] b = new byte[1024 * 5]; // 5KB                    // int len;                    // while ((len = in.read(b)) != -1) {                    // out.write(b, 0, len);                    // }                    // out.flush();                    // in.close();                    // out.close();                    // 内容流读                    AssetFileDescriptor afd = getContentResolver()                            .openAssetFileDescriptor(                                    Uri.parse("content://com.h3c.test/h3c.txt"),                                    "wr");                    OutputStream out = afd.createOutputStream();                    FileInputStream in = new FileInputStream(new File(                            "/mnt/sdcard/h3c2.txt"));                    byte[] b = new byte[1024 * 5]; // 5KB                    int len;                    while ((len = in.read(b)) != -1) {                        out.write(b, 0, len);                    }                    out.flush();                    in.close();                    out.close();                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }}


热点排行