編輯:關於Android編程
我們知道Android兩個應用程序之間進行數據交互需要通過ContentProvider,而且通常都是數據庫的操作。
今天項目需要使用Android的ContentProvider交互普通SD卡上的文件,於是我寫了這個小例子:
AndroidManifest.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.h3c.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".NotepadTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name=".TestContentProvider" android:authorities="com.h3c.test" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.h3c.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".NotepadTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name=".TestContentProvider" android:authorities="com.h3c.test" />
</application>
</manifest>
TestContentProvider.java
[java]
package com.h3c.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class TestContentProvider extends ContentProvider {
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
Log.e("H3c", "delete");
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
Log.e("H3c", "gettype");
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
Log.e("H3c", "insert");
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
Log.e("H3c", "create");
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
Log.e("H3c", "query");
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
Log.e("H3c", "update");
return 0;
}
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
Log.e("H3c", "openAssetFile");
return super.openAssetFile(uri, mode);
}
//此方法非常重要,一定要重寫,否則默認報FileNotFound異常
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
File root = Environment.getExternalStorageDirectory();
root.mkdirs();
File path = new File(root, uri.getEncodedPath());
Log.e("H3c", "opeFile:"+path);
int imode = 0;
if (mode.contains("w")) {
imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
if (!path.exists()) {
try {
path.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (mode.contains("r"))
imode |= ParcelFileDescriptor.MODE_READ_ONLY;
if (mode.contains("+"))
imode |= ParcelFileDescriptor.MODE_APPEND;
return ParcelFileDescriptor.open(path, imode);
}
}
package com.h3c.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class TestContentProvider extends ContentProvider {
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
Log.e("H3c", "delete");
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
Log.e("H3c", "gettype");
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
Log.e("H3c", "insert");
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
Log.e("H3c", "create");
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
Log.e("H3c", "query");
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
Log.e("H3c", "update");
return 0;
}
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
Log.e("H3c", "openAssetFile");
return super.openAssetFile(uri, mode);
}
//此方法非常重要,一定要重寫,否則默認報FileNotFound異常
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
// TODO Auto-generated method stub
File root = Environment.getExternalStorageDirectory();
root.mkdirs();
File path = new File(root, uri.getEncodedPath());
Log.e("H3c", "opeFile:"+path);
int imode = 0;
if (mode.contains("w")) {
imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
if (!path.exists()) {
try {
path.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (mode.contains("r"))
imode |= ParcelFileDescriptor.MODE_READ_ONLY;
if (mode.contains("+"))
imode |= ParcelFileDescriptor.MODE_APPEND;
return ParcelFileDescriptor.open(path, imode);
}
}
NotepadTestActivity.java
[java]
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();
}
}
});
}
}
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();
}
}
});
}
}
作者:h3c4lenovo
在做Android 開發過程中,會遇到一些小的問題,雖然自己動手也能解決,但是有了一些小工具,解決這些問題就得心應手了,今天就為大家推薦一下Android 開發遇到的小工
不知道大家有沒有用過,淘寶電影客戶端(淘票票)買過電影票,縱觀各類在線選座app的在線選座功能 淘寶在線選座功能用戶體驗最好,用起來最順手,誇張點說已經到了爐火純青的地步
自定義日歷控件,支持舊歷、節氣、日期標注、點擊操作 (參考網絡上的日歷控件改寫)注:將下面的四張資源圖片拷貝到所建包的下一個image目錄中,如Calendar.java
本android項目是致力於建立一個本地的sqlite數據庫,然後存儲考試的題目,android應用調用sqlite數據庫中的數據,生成相應的答題界面。下面是代碼解析&n