我需要将我的SQLite数据库附加到Gmail电子邮件中。
我认为使用内容提供商是必要的,但我无法弄清楚这一点。请帮我理解这个概念。
我的数据库位于默认目录"data/data/packagename/databases/"
我的数据库名称是 TBProvider 。
扩展ContentProvider的类也称为 TBProvider 。
TBProvider
public class TBProvider extends ContentProvider {
static String PROVIDER_NAME =
"com.sas.aap.tbprovider";
public static Uri CONTENT_URI =
Uri.parse("content://com.sas.aap.tbprovider");
public static final String _ID = "_id";
private static final int TB = 1;
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "*", TB);
}
public static final String DATABASE_NAME = "TBProvider";
private SQLiteDatabase myTBProvider;
private static class TBDBHelper extends SQLiteOpenHelper{
TBDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case TB:
return "vnd.android.cursor.dir/com.sas.aap.tbprovider ";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
TBDBHelper dbHelper = new TBDBHelper(context);
myTBProvider = dbHelper.getWritableDatabase();
return (myTBDatabase == null)? false:true;
}
}
电子邮件:
public class Email extends Activity {
String personsEmail = "example@gmail.com";
EditText personsName;
String emailAdd, name;
Button sendEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
sendEmail = (Button) findViewById(R.id.bSEmail);
sendEmail.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String subject = "Subject";
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/com.sas.aap.tbprovider ");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
TBProvider.CONTENT_URI);
startActivity(emailIntent);
}
});
}
}
清单:
android:name="com.sas.aap.TBProvider"
android:authorities="com.sas.aap.tbprovider"
android:exported="true">
修改
我得到以下LogCat:
Writing exception to parcel
java.lang.IllegalArgumentException: Unsupported URI: content://com.sas.aap.tbprovider
at com.sas.aap.TBProvider.getType(TBProvider.java130)
at android.content.ContentProvider$Transport.getType(TBProvider.java:130)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:135)
at android.os.Binder.execTransact(Binder.java:367)
at dalvik.system.NativeStart.run(Native Method)
我需要通过超级碗完成这个!如果您需要更多信息,请告诉我。我将会 从现在开始醒来,直到我能解决这个问题。谢谢,麻烦您了!
答案 0 :(得分:0)
我想出来了!
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File cacheDir = getContext().getDatabasePath(DATABASE_NAME);
return ParcelFileDescriptor.open(cacheDir, ParcelFileDescriptor.MODE_READ_ONLY);