// 根据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])); }
/** * 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 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); } } }