1

我需要將我的SQLite數據庫附加到Gmail電子郵件。將Android SQLite數據庫附加到Gmail

我相信使用Content Provider是必要的,但我無法弄清楚這一點。請幫我理解這個概念。

我的數據庫位於默認目錄"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 = "[email protected]"; 
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

我發現了!

public ParcelFileDescriptor openFile(Uri uri, String mode) 
     throws FileNotFoundException { 
    File cacheDir = getContext().getDatabasePath(DATABASE_NAME);  
    return ParcelFileDescriptor.open(cacheDir, ParcelFileDescriptor.MODE_READ_ONLY);