/** * Initiates the interaction. This may result in a phone call or sms message started or a * disambiguation dialog to determine which phone number should be used. */ @VisibleForTesting /* package */ void startInteraction(Uri uri) { if (mLoader != null) { mLoader.reset(); } final Uri queryUri; final String inputUriAsString = uri.toString(); if (inputUriAsString.startsWith(Contacts.CONTENT_URI.toString())) { if (!inputUriAsString.endsWith(Contacts.Data.CONTENT_DIRECTORY)) { queryUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY); } else { queryUri = uri; } } else if (inputUriAsString.startsWith(Data.CONTENT_URI.toString())) { queryUri = uri; } else { throw new UnsupportedOperationException( "Input Uri must be contact Uri or data Uri (input: \"" + uri + "\")"); } mLoader = new CursorLoader( this, queryUri, PHONE_NUMBER_PROJECTION, PHONE_NUMBER_SELECTION, null, null); mLoader.registerListener(0, this); mLoader.startLoading(); }
@Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT): if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); CursorLoader loader = new CursorLoader(this, contactData, null, null, null, null); loader.registerListener( LOADER_ID_CONTACT, new Loader.OnLoadCompleteListener<Cursor>() { @Override public void onLoadComplete(final Loader<Cursor> loader, final Cursor data) { if (data.moveToFirst()) { int nameColumnIndex = data.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY); String name = data.getString(nameColumnIndex); txtContacts.setText(name); } } }); loader.startLoading(); } break; } }
// 根据uri获得绝对路径(原理不懂) public static String getPath(Context context, Uri uri) { String[] pojo = {MediaStore.Images.Media.DATA}; CursorLoader cursorLoader = new CursorLoader(context, uri, pojo, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); cursor.moveToFirst(); return cursor.getString(cursor.getColumnIndex(pojo[0])); }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CursorLoader cursorLoader = new CursorLoader( getActivity(), EntryColumns.CONTENT_URI(mEntriesIds[id]), null, null, null, null); cursorLoader.setUpdateThrottle(1000); return cursorLoader; }
/** * Takes a uri, and from it finds the absolute path to the media. It returns the absolute path as * a string. </br></br> * * <p>CODE REUSE </br> URL: * http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore </br> * DATE: NOV. 9, 2013 </br> License: CC- </br> * * @param contentUri Uri we want to find the path of. Note that it is of type Uri and not URI. * @param context * @return the absolute path of the Uri as a string */ private String getRealPathFromURI(Uri contentUri, Context context) { String[] proj = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
public static Uri getLastRecordedVideoUri(Activity activity) { String[] proj = {MediaStore.Video.Media._ID}; Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; String sortOrder = MediaStore.Video.VideoColumns.DATE_TAKEN + " DESC"; CursorLoader loader = new CursorLoader(activity, contentUri, proj, null, null, sortOrder); Cursor cursor = loader.loadInBackground(); cursor.moveToFirst(); return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0)); }
// 获得照片的真实路径 public static String obtainFilePath(Context context, Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; // Cursor cursor = managedQuery(uri, projection, null, null, null);//deprecated CursorLoader cursorLoader = new CursorLoader(context, uri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); Log.e(TAG, "文件真实路径:" + path); cursor.close(); return path; }
private String getRealPathFromURI(Uri contentUri) { String[] proj = {MediaStore.Video.Media.DATA}; String result = ""; try { CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } catch (Exception e) { e.printStackTrace(); } return result; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; CursorLoader cursorLoader = new CursorLoader(getApplicationContext(), contactUri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String number = cursor.getString(numberColumn); int nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); String name = cursor.getString(nameColumn); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); Editor editor = sharedPreferences.edit(); editor.putString(name, number); editor.commit(); if (!listItems.contains(name)) { listItems.add(name); adapter.notifyDataSetChanged(); } // ListView contactList = (ListView) findViewById(R.id.contactView); Log.i(TAG, "Phone number: " + String.valueOf(number)); // Do something with the phone number... } } }
private Cursor getCursor() { Cursor cursor; String[] projection = { MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, "count(" + MediaStore.Images.Media._ID + ")" }; String selection = " 0 == 0) group by " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " -- ("; CursorLoader cLoader = new CursorLoader( this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null); cursor = cLoader.loadInBackground(); return cursor; }
/** 把uri转为File对象 */ public static File uri2File(Activity aty, Uri uri) { if (android.os.Build.VERSION.SDK_INT < 11) { // 在API11以下可以使用:managedQuery String[] proj = {MediaStore.Images.Media.DATA}; @SuppressWarnings("deprecation") Cursor actualimagecursor = aty.managedQuery(uri, proj, null, null, null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index); return new File(img_path); } else { // 在API11以上:要转为使用CursorLoader,并使用loadInBackground来返回 String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(aty, uri, projection, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return new File(cursor.getString(column_index)); } }
@Override public void configureLoader(CursorLoader loader, long directoryId) { loader.setUri(Phones.CONTENT_URI); loader.setProjection(PHONES_PROJECTION); loader.setSortOrder(Phones.DISPLAY_NAME); // begin: added by yunzhou.song StringBuilder selection = new StringBuilder(); if (loader.getSelection() != null) { selection.append(loader.getSelection()); } if (mExcludeUris != null && mExcludeUris.length > 0) { if (selection.length() > 0) { selection.append(" AND "); } selection.append(Phone._ID + " NOT IN ("); for (int i = 0; i < mExcludeUris.length; i++) { selection.append(((Uri) mExcludeUris[i]).getLastPathSegment()).append(","); } selection.deleteCharAt(selection.length() - 1); selection.append(")"); } loader.setSelection(selection.toString()); // end: added by yunzhou.song }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == REQUEST_CAMERA) { // setImageView(); String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String picturePath = cursor.getString(column_index_data); } else if (requestCode == SELECT_FILE) { Uri selectedImageUri = data.getData(); String[] projection = {MediaStore.MediaColumns.DATA}; CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); String selectedImagePath = cursor.getString(column_index); Bitmap bm; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(selectedImagePath, options); final int REQUIRED_SIZE = 200; int scale = 1; while (options.outWidth / scale / 2 >= REQUIRED_SIZE && options.outHeight / scale / 2 >= REQUIRED_SIZE) scale *= 2; options.inSampleSize = scale; options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(selectedImagePath, options); imageView.setImageBitmap(bm); } } }
public static void loadPhoneContacts( Context context, final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) { final String[] PROJECTION = new String[] { ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.PHOTO_URI, ContactsContract.Data.LOOKUP_KEY, ContactsContract.CommonDataKinds.Im.DATA }; final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\"" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE + "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL + "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER + "\")"; CursorLoader mCursorLoader = new CursorLoader( context, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null); mCursorLoader.registerListener( 0, new OnLoadCompleteListener<Cursor>() { @Override public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) { if (cursor == null) { return; } while (cursor.moveToNext()) { Bundle contact = new Bundle(); contact.putInt( "phoneid", cursor.getInt(cursor.getColumnIndex(ContactsContract.Data._ID))); contact.putString( "displayname", cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))); contact.putString( "photouri", cursor.getString(cursor.getColumnIndex(ContactsContract.Data.PHOTO_URI))); contact.putString( "lookup", cursor.getString(cursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY))); contact.putString( "jid", cursor.getString( cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA))); phoneContacts.add(contact); } if (listener != null) { listener.onPhoneContactsLoaded(phoneContacts); } cursor.close(); } }); try { mCursorLoader.startLoading(); } catch (RejectedExecutionException e) { if (listener != null) { listener.onPhoneContactsLoaded(phoneContacts); } } }