Created
November 25, 2013 09:47
-
-
Save shikajiro/7638946 to your computer and use it in GitHub Desktop.
ContentProviderのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.contentprovidersample; | |
import android.app.Activity; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
import android.view.Menu; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String[] projection = new String[]{ | |
MediaStore.MediaColumns.TITLE, | |
MediaStore.MediaColumns.DATA, | |
}; | |
// ContactsContract.CommonDataKinds.Phone.CONTENT_URI | |
Cursor cursor = getContentResolver().query( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | |
projection, | |
null/*絞込パラメータ*/, | |
null/*絞込データ*/, | |
null/*並び替え*/); | |
if(cursor != null && 0 < cursor.getCount()){ | |
cursor.moveToFirst();//あってもなくてもいい | |
int titleIndex = cursor.getColumnIndex(MediaStore.MediaColumns.TITLE); | |
int pathIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA); | |
do{ | |
Log.i("contentprovider", "title:"+cursor.getString(titleIndex)); | |
Log.i("contentprovider", "path:"+cursor.getString(pathIndex)); | |
}while(cursor.moveToNext()); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment