1

I need to attach my SQLite Database to a Gmail email.

I believe the use of a Content Provider is necessary, but I cannot figure this out. Please help me understand this concept.

My db is located in the default directory "data/data/packagename/databases/"

My database name is TBProvider.

The class that extends ContentProvider is also named 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;                  
    }
}

Email:

 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);

            }
        });
    }
}

Manifest:

      android:name="com.sas.aap.TBProvider"
      android:authorities="com.sas.aap.tbprovider"
      android:exported="true">

EDIT:

I am getting the following 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)

I need to finish this by the Super Bowl! Let me know if you need more info. I'll be Awake from now until I can get this solved. Please and Thank You!

4

1 回答 1

0

我想到了!

public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    File cacheDir = getContext().getDatabasePath(DATABASE_NAME);    
    return ParcelFileDescriptor.open(cacheDir, ParcelFileDescriptor.MODE_READ_ONLY);                    
于 2013-02-03T04:32:00.593 回答