最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)拍照或者選取本地圖片

 更新時(shí)間:2022年03月29日 18:55:03   作者:kevinjqy  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照或者選取本地圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)拍照或者選取本地圖片的具體代碼,供大家參考,具體內(nèi)容如下

總體流程

從selectPhotoActivity中啟動(dòng)圖冊(cè)或者相機(jī),再根據(jù)獲取的uri進(jìn)行裁剪,返回uri,再對(duì)這個(gè)uri執(zhí)行一系列操縱。

從相冊(cè)選取圖片

private void pickPhoto() {
? ? ? ? Intent intent = new Intent(Intent.ACTION_PICK,
? ? ? ? ? ? ? ? android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
? ? }

使用隱式intent啟動(dòng)相機(jī)。

拍照獲取圖片

private void takePhoto() {
? ? ? ? // 執(zhí)行拍照前,應(yīng)該先判斷SD卡是否存在
? ? ? ? String SDState = Environment.getExternalStorageState();
? ? ? ? if (SDState.equals(Environment.MEDIA_MOUNTED)) {
? ? ? ? ? ? Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
? ? ? ? ? ? File path = Environment
? ? ? ? ? ? ? ? ? ? .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
? ? ? ? ? ? File file = new File(path, IMAGE_FILE_NAME);
? ? ? ? ? ? takePhoto = Uri.fromFile(file);
? ? ? ? ? ? intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
? ? ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
? ? ? ? } else {
? ? ? ? ? ? Toast.makeText(getApplicationContext(), "內(nèi)存卡不存在",
? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }

值得注意的一點(diǎn)是,intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto)中,設(shè)置了拍完照照片的存放路徑takePhoto,在此情況下,部分機(jī)型的onActivityResult()中不會(huì)返回?cái)?shù)據(jù),即data.getData()為空,因?yàn)榭梢愿鶕?jù)存放路徑即可獲取拍照?qǐng)D片。

裁剪圖片

public void startPhotoZoom(Uri uri) {
? ? ? ? Intent intent = new Intent("com.android.camera.action.CROP");
? ? ? ? intent.setDataAndType(uri, "image/*");
? ? ? ? // 設(shè)置裁剪
? ? ? ? intent.putExtra("crop", "true");
? ? ? ? // aspectX aspectY 是寬高的比例
? ? ? ? intent.putExtra("aspectX", 1);
? ? ? ? intent.putExtra("aspectY", 1);
? ? ? ? // outputX outputY 是裁剪圖片寬高
? ? ? ? intent.putExtra("outputX", 340);
? ? ? ? intent.putExtra("outputY", 340);
? ? ? ? //將URI指向相應(yīng)的file:///…
? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
? ? ? ? // 不返回圖片文件
? ? ? ? intent.putExtra("return-data", false);
? ? ? ? startActivityForResult(intent, RESULT_REQUEST_CODE);
? ? }

裁剪方法調(diào)用android自帶的裁剪庫,部分深度定制的機(jī)型,如魅族,可能不存在該庫,那么就需要自定義或者使用開源裁剪庫。

返回的數(shù)據(jù)的處理

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? ? ? if (resultCode != Activity.RESULT_OK) {
? ? ? ? ? ? Log.e("TAG","ActivityResult resultCode error");
? ? ? ? ? ? return;
? ? ? ? }

? ? ? ? switch (requestCode){
? ? ? ? ? ? case SELECT_PIC_BY_PICK_PHOTO:
? ? ? ? ? ? ? ? Uri uri = data.getData();
? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(uri.getAuthority())) {
? ? ? ? ? ? ? ? ? ? //查詢選擇圖片
? ? ? ? ? ? ? ? ? ? Cursor cursor = getContentResolver().query(
? ? ? ? ? ? ? ? ? ? ? ? uri,
? ? ? ? ? ? ? ? ? ? ? ? new String[] { MediaStore.Images.Media.DATA },
? ? ? ? ? ? ? ? ? ? ? ? null,
? ? ? ? ? ? ? ? ? ? ? ? null,
? ? ? ? ? ? ? ? ? ? ? ? null);
? ? ? ? ? ? ? ? ? ? //返回 沒找到選擇圖片
? ? ? ? ? ? ? ? ? ? if (null == cursor) {
? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //光標(biāo)移動(dòng)至開頭 獲取圖片路徑
? ? ? ? ? ? ? ? ? ? cursor.moveToFirst();
? ? ? ? ? ? ? ? ? ? picPath = cursor.getString(cursor
? ? ? ? ? ? ? ? ? ? ? ? .getColumnIndex(MediaStore.Images.Media.DATA));
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑啊啊啊啊啊啊",picPath);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case SELECT_PIC_BY_TACK_PHOTO:
? ? ? ? ? ? ? ? //裁剪圖片
? ? ? ? ? ? ? ? startPhotoZoom(takePhoto);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case RESULT_REQUEST_CODE :
? ? ? ? ? ? ? ? if (data != null) {
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑",data.getData().toString());
? ? ? ? ? ? ? ? ? ? picPath = getPathByUri4kitkat(getApplicationContext(),data.getData());
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑啊啊啊啊啊啊",picPath);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if(requestCode != SELECT_PIC_BY_TACK_PHOTO) {
? ? ? ? ? ? lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
? ? ? ? ? ? setResult(Activity.RESULT_OK, lastIntent);
? ? ? ? ? ? finish();
? ? ? ? }
? ? ? ? super.onActivityResult(requestCode, resultCode, data);

? ? }

因?yàn)樵诒綼ctivity中可能啟動(dòng)三個(gè)新的activity,即拍照activity,相冊(cè)activity,裁剪activity。需要注意,拍完照的圖片需要經(jīng)過裁剪,即,只有從相冊(cè)選取和裁剪返回的數(shù)據(jù)可以setRuselt(),故需要添加一個(gè)if語句加以判別。

根據(jù)Uri獲取真實(shí)路徑

還是因?yàn)闄C(jī)型適配的問題,以下提供兩種方法,大家自己嘗試:

方法一

public static String getRealPathFromURI(final Context context, final Uri uri ) {
? ? ? ? if ( null == uri ) return null;
? ? ? ? final String scheme = uri.getScheme();
? ? ? ? String data = null;
? ? ? ? if ( scheme == null )
? ? ? ? ? ? data = uri.getPath();
? ? ? ? else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
? ? ? ? ? ? data = uri.getPath();
? ? ? ? } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
? ? ? ? ? ? Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
? ? ? ? ? ? if ( null != cursor ) {
? ? ? ? ? ? ? ? if ( cursor.moveToFirst() ) {
? ? ? ? ? ? ? ? ? ? int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
? ? ? ? ? ? ? ? ? ? if ( index > -1 ) {
? ? ? ? ? ? ? ? ? ? ? ? data = cursor.getString( index );
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? cursor.close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return data;
? ? }

方法二

@SuppressLint("NewApi")
? ? public static String getPathByUri4kitkat(final Context context, final Uri uri) {
? ? ? ? final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
? ? ? ? // DocumentProvider
? ? ? ? if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
? ? ? ? ? ? if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final String[] split = docId.split(":");
? ? ? ? ? ? ? ? final String type = split[0];
? ? ? ? ? ? ? ? if ("primary".equalsIgnoreCase(type)) {
? ? ? ? ? ? ? ? ? ? return Environment.getExternalStorageDirectory() + "/" + split[1];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else if (isDownloadsDocument(uri)) {// DownloadsProvider
? ? ? ? ? ? ? ? final String id = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
? ? ? ? ? ? ? ? ? ? ? ? Long.valueOf(id));
? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, null, null);
? ? ? ? ? ? } else if (isMediaDocument(uri)) {// MediaProvider
? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final String[] split = docId.split(":");
? ? ? ? ? ? ? ? final String type = split[0];
? ? ? ? ? ? ? ? Uri contentUri = null;
? ? ? ? ? ? ? ? if ("image".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? } else if ("video".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? } else if ("audio".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? final String selection = "_id=?";
? ? ? ? ? ? ? ? final String[] selectionArgs = new String[] { split[1] };
? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, selection, selectionArgs);
? ? ? ? ? ? }
? ? ? ? } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
? ? ? ? ? ? // (and
? ? ? ? ? ? // general)
? ? ? ? ? ? return getDataColumn(context, uri, null, null);
? ? ? ? } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
? ? ? ? ? ? return uri.getPath();
? ? ? ? }
? ? ? ? return null;
? ? }

? ? /**
? ? ?* Get the value of the data column for this Uri. This is useful for
? ? ?* MediaStore Uris, and other file-based ContentProviders.
? ? ?*
? ? ?* @param context
? ? ?* ? ? ? ? ? ?The context.
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to query.
? ? ?* @param selection
? ? ?* ? ? ? ? ? ?(Optional) Filter used in the query.
? ? ?* @param selectionArgs
? ? ?* ? ? ? ? ? ?(Optional) Selection arguments used in the query.
? ? ?* @return The value of the _data column, which is typically a file path.
? ? ?*/
? ? public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
? ? ? ? Cursor cursor = null;
? ? ? ? final String column = "_data";
? ? ? ? final String[] projection = { column };
? ? ? ? try {
? ? ? ? ? ? cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
? ? ? ? ? ? if (cursor != null && cursor.moveToFirst()) {
? ? ? ? ? ? ? ? final int column_index = cursor.getColumnIndexOrThrow(column);
? ? ? ? ? ? ? ? return cursor.getString(column_index);
? ? ? ? ? ? }
? ? ? ? } finally {
? ? ? ? ? ? if (cursor != null)
? ? ? ? ? ? ? ? cursor.close();
? ? ? ? }
? ? ? ? return null;
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is ExternalStorageProvider.
? ? ?*/
? ? public static boolean isExternalStorageDocument(Uri uri) {
? ? ? ? return "com.android.externalstorage.documents".equals(uri.getAuthority());
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is DownloadsProvider.
? ? ?*/
? ? public static boolean isDownloadsDocument(Uri uri) {
? ? ? ? return "com.android.providers.downloads.documents".equals(uri.getAuthority());
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is MediaProvider.
? ? ?*/
? ? public static boolean isMediaDocument(Uri uri) {
? ? ? ? return "com.android.providers.media.documents".equals(uri.getAuthority());
? ? }

整體代碼

public class selectPhotoActivity extends Activity implements View.OnClickListener{

? ? /** 使用照相機(jī)拍照獲取圖片 */
? ? public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
? ? /** 使用相冊(cè)中的圖片 */
? ? public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
? ? /** 裁剪圖片 */
? ? private static final int RESULT_REQUEST_CODE = 3;
? ? /** 開啟相機(jī) */
? ? private Button btn_take_photo;
? ? /** 開啟圖冊(cè) */
? ? private Button btn_pick_photo;
? ? /** 取消 */
? ? private Button btn_cancel;
? ? /** 圖片名稱 */
? ? private static final String IMAGE_FILE_NAME = "image.jpg";
? ? /** 獲取到的圖片路徑 */
? ? private String picPath;
? ? //保存裁剪后的圖像
? ? private Bitmap photo;
? ? private Intent lastIntent;
? ? private Uri takePhoto;
? ? /** 從Intent獲取圖片路徑的KEY */
? ? public static final String KEY_PHOTO_PATH = "photo_path";
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_select_photo);

? ? ? ? btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
? ? ? ? btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
? ? ? ? btn_cancel = (Button) findViewById(R.id.btn_cancel);

? ? ? ? lastIntent = getIntent();

? ? ? ? btn_take_photo.setOnClickListener(this);
? ? ? ? btn_pick_photo.setOnClickListener(this);
? ? ? ? btn_cancel.setOnClickListener(this);
? ? }

? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.btn_take_photo : // 開啟相機(jī)
? ? ? ? ? ? ? ? takePhoto();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_pick_photo : // 開啟圖冊(cè)
? ? ? ? ? ? ? ? pickPhoto();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_cancel : // 取消操作
? ? ? ? ? ? ? ? this.finish();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default :
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }

? ? /**
? ? ?* 拍照獲取圖片
? ? ?*/
? ? private void takePhoto() {
? ? ? ? // 執(zhí)行拍照前,應(yīng)該先判斷SD卡是否存在
? ? ? ? String SDState = Environment.getExternalStorageState();
? ? ? ? if (SDState.equals(Environment.MEDIA_MOUNTED)) {
? ? ? ? ? ? Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
? ? ? ? ? ? File path = Environment
? ? ? ? ? ? ? ? ? ? .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
? ? ? ? ? ? File file = new File(path, IMAGE_FILE_NAME);
? ? ? ? ? ? takePhoto = Uri.fromFile(file);
? ? ? ? ? ? intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
? ? ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
? ? ? ? } else {
? ? ? ? ? ? Toast.makeText(getApplicationContext(), "內(nèi)存卡不存在",
? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }

? ? /***
? ? ?* 從相冊(cè)中取圖片
? ? ?*/
? ? private void pickPhoto() {
? ? ? ? Intent intent = new Intent(Intent.ACTION_PICK,
? ? ? ? ? ? ? ? android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
? ? }

? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? finish();
? ? ? ? return super.onTouchEvent(event);
? ? }

? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? ? ? if (resultCode != Activity.RESULT_OK) {
? ? ? ? ? ? Log.e("TAG","ActivityResult resultCode error");
? ? ? ? ? ? return;
? ? ? ? }

? ? ? ? switch (requestCode){
? ? ? ? ? ? case SELECT_PIC_BY_PICK_PHOTO:
? ? ? ? ? ? ? ? Uri uri = data.getData();
? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(uri.getAuthority())) {
? ? ? ? ? ? ? ? ? ? //查詢選擇圖片
? ? ? ? ? ? ? ? ? ? Cursor cursor = getContentResolver().query(
? ? ? ? ? ? ? ? ? ? ? ? uri,
? ? ? ? ? ? ? ? ? ? ? ? new String[] { MediaStore.Images.Media.DATA },
? ? ? ? ? ? ? ? ? ? ? ? null,
? ? ? ? ? ? ? ? ? ? ? ? null,
? ? ? ? ? ? ? ? ? ? ? ? null);
? ? ? ? ? ? ? ? ? ? //返回 沒找到選擇圖片
? ? ? ? ? ? ? ? ? ? if (null == cursor) {
? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //光標(biāo)移動(dòng)至開頭 獲取圖片路徑
? ? ? ? ? ? ? ? ? ? cursor.moveToFirst();
? ? ? ? ? ? ? ? ? ? picPath = cursor.getString(cursor
? ? ? ? ? ? ? ? ? ? ? ? .getColumnIndex(MediaStore.Images.Media.DATA));
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑啊啊啊啊啊啊",picPath);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case SELECT_PIC_BY_TACK_PHOTO:
? ? ? ? ? ? ? ? //裁剪圖片
? ? ? ? ? ? ? ? startPhotoZoom(takePhoto);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case RESULT_REQUEST_CODE :
? ? ? ? ? ? ? ? if (data != null) {
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑",data.getData().toString());
? ? ? ? ? ? ? ? ? ? picPath = getPathByUri4kitkat(getApplicationContext(),data.getData());
? ? ? ? ? ? ? ? ? ? Log.d("圖片路徑啊啊啊啊啊啊",picPath);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if(requestCode != SELECT_PIC_BY_TACK_PHOTO) {
? ? ? ? ? ? lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
? ? ? ? ? ? setResult(Activity.RESULT_OK, lastIntent);
? ? ? ? ? ? finish();
? ? ? ? }
? ? ? ? super.onActivityResult(requestCode, resultCode, data);

? ? }

? ? /**
? ? ?* 裁剪圖片方法實(shí)現(xiàn)
? ? ?*
? ? ?* @param uri
? ? ?*/
? ? public void startPhotoZoom(Uri uri) {
? ? ? ? Intent intent = new Intent("com.android.camera.action.CROP");
? ? ? ? intent.setDataAndType(uri, "image/*");
? ? ? ? // 設(shè)置裁剪
? ? ? ? intent.putExtra("crop", "true");
? ? ? ? // aspectX aspectY 是寬高的比例
? ? ? ? intent.putExtra("aspectX", 1);
? ? ? ? intent.putExtra("aspectY", 1);
? ? ? ? // outputX outputY 是裁剪圖片寬高
? ? ? ? intent.putExtra("outputX", 340);
? ? ? ? intent.putExtra("outputY", 340);
? ? ? ? //將URI指向相應(yīng)的file:///…
? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
? ? ? ? // 不返回圖片文件
? ? ? ? intent.putExtra("return-data", false);
? ? ? ? startActivityForResult(intent, RESULT_REQUEST_CODE);
? ? }

? ? //Android 4.4后通過Uri獲取路徑以及文件名一種方法
? ? public static String getRealPathFromURI(final Context context, final Uri uri ) {
? ? ? ? if ( null == uri ) return null;
? ? ? ? final String scheme = uri.getScheme();
? ? ? ? String data = null;
? ? ? ? if ( scheme == null )
? ? ? ? ? ? data = uri.getPath();
? ? ? ? else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
? ? ? ? ? ? data = uri.getPath();
? ? ? ? } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
? ? ? ? ? ? Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
? ? ? ? ? ? if ( null != cursor ) {
? ? ? ? ? ? ? ? if ( cursor.moveToFirst() ) {
? ? ? ? ? ? ? ? ? ? int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
? ? ? ? ? ? ? ? ? ? if ( index > -1 ) {
? ? ? ? ? ? ? ? ? ? ? ? data = cursor.getString( index );
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? cursor.close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return data;
? ? }

? ? // 專為Android4.4設(shè)計(jì)的從Uri獲取文件絕對(duì)路徑,以前的方法已不好使
? ? @SuppressLint("NewApi")
? ? public static String getPathByUri4kitkat(final Context context, final Uri uri) {
? ? ? ? final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
? ? ? ? // DocumentProvider
? ? ? ? if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
? ? ? ? ? ? if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final String[] split = docId.split(":");
? ? ? ? ? ? ? ? final String type = split[0];
? ? ? ? ? ? ? ? if ("primary".equalsIgnoreCase(type)) {
? ? ? ? ? ? ? ? ? ? return Environment.getExternalStorageDirectory() + "/" + split[1];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else if (isDownloadsDocument(uri)) {// DownloadsProvider
? ? ? ? ? ? ? ? final String id = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
? ? ? ? ? ? ? ? ? ? ? ? Long.valueOf(id));
? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, null, null);
? ? ? ? ? ? } else if (isMediaDocument(uri)) {// MediaProvider
? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
? ? ? ? ? ? ? ? final String[] split = docId.split(":");
? ? ? ? ? ? ? ? final String type = split[0];
? ? ? ? ? ? ? ? Uri contentUri = null;
? ? ? ? ? ? ? ? if ("image".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? } else if ("video".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? } else if ("audio".equals(type)) {
? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? final String selection = "_id=?";
? ? ? ? ? ? ? ? final String[] selectionArgs = new String[] { split[1] };
? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, selection, selectionArgs);
? ? ? ? ? ? }
? ? ? ? } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
? ? ? ? ? ? // (and
? ? ? ? ? ? // general)
? ? ? ? ? ? return getDataColumn(context, uri, null, null);
? ? ? ? } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
? ? ? ? ? ? return uri.getPath();
? ? ? ? }
? ? ? ? return null;
? ? }

? ? /**
? ? ?* Get the value of the data column for this Uri. This is useful for
? ? ?* MediaStore Uris, and other file-based ContentProviders.
? ? ?*
? ? ?* @param context
? ? ?* ? ? ? ? ? ?The context.
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to query.
? ? ?* @param selection
? ? ?* ? ? ? ? ? ?(Optional) Filter used in the query.
? ? ?* @param selectionArgs
? ? ?* ? ? ? ? ? ?(Optional) Selection arguments used in the query.
? ? ?* @return The value of the _data column, which is typically a file path.
? ? ?*/
? ? public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
? ? ? ? Cursor cursor = null;
? ? ? ? final String column = "_data";
? ? ? ? final String[] projection = { column };
? ? ? ? try {
? ? ? ? ? ? cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
? ? ? ? ? ? if (cursor != null && cursor.moveToFirst()) {
? ? ? ? ? ? ? ? final int column_index = cursor.getColumnIndexOrThrow(column);
? ? ? ? ? ? ? ? return cursor.getString(column_index);
? ? ? ? ? ? }
? ? ? ? } finally {
? ? ? ? ? ? if (cursor != null)
? ? ? ? ? ? ? ? cursor.close();
? ? ? ? }
? ? ? ? return null;
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is ExternalStorageProvider.
? ? ?*/
? ? public static boolean isExternalStorageDocument(Uri uri) {
? ? ? ? return "com.android.externalstorage.documents".equals(uri.getAuthority());
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is DownloadsProvider.
? ? ?*/
? ? public static boolean isDownloadsDocument(Uri uri) {
? ? ? ? return "com.android.providers.downloads.documents".equals(uri.getAuthority());
? ? }

? ? /**
? ? ?* @param uri
? ? ?* ? ? ? ? ? ?The Uri to check.
? ? ?* @return Whether the Uri authority is MediaProvider.
? ? ?*/
? ? public static boolean isMediaDocument(Uri uri) {
? ? ? ? return "com.android.providers.media.documents".equals(uri.getAuthority());
? ? }

}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android多線程下載示例詳解

    Android多線程下載示例詳解

    這篇文章主要為大家詳細(xì)介紹了Android多線程下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android 調(diào)試工具用法詳細(xì)介紹

    Android 調(diào)試工具用法詳細(xì)介紹

    本文主要介紹Android 調(diào)試工具,在Android應(yīng)用開發(fā)的過程中肯定會(huì)用到Android 調(diào)試工具,這里整理了調(diào)試工具的使用方法,有需要的小伙伴可以參考下
    2016-08-08
  • Android 保存Fragment 切換狀態(tài)實(shí)例代碼

    Android 保存Fragment 切換狀態(tài)實(shí)例代碼

    本文主要介紹Android Fragment的應(yīng)用,這里給大家用實(shí)例代碼詳細(xì)介紹了Android Fragment 切換狀態(tài),有需要的小伙伴可以參考下
    2016-07-07
  • Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動(dòng)自動(dòng)播放

    Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動(dòng)自動(dòng)播放

    這篇文章主要介紹了Android 使用ViewPager實(shí)現(xiàn)圖片左右循環(huán)滑動(dòng)自動(dòng)播放的相關(guān)資料,非常不錯(cuò),具有參考解決價(jià)值,需要的朋友可以參考下
    2016-08-08
  • ProgressBar、ProgessDialog-用法(詳解)

    ProgressBar、ProgessDialog-用法(詳解)

    下面小編就為大家?guī)硪黄狿rogressBar、ProgessDialog-用法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 如何在Android中實(shí)現(xiàn)左右滑動(dòng)的指引效果

    如何在Android中實(shí)現(xiàn)左右滑動(dòng)的指引效果

    本篇文章是對(duì)在Android中實(shí)現(xiàn)左右滑動(dòng)指引效果的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android仿微信支付密碼彈出層功能

    Android仿微信支付密碼彈出層功能

    最近項(xiàng)目中使用到了支付密碼功能,感覺這類界面應(yīng)該是比較常用的,涉及支付密碼的輸入的一般都會(huì)用到吧,所以單獨(dú)地把這部分抽取出來,有需要的朋友可以拿去用用
    2017-04-04
  • Android開發(fā)之如何自定義數(shù)字鍵盤詳解

    Android開發(fā)之如何自定義數(shù)字鍵盤詳解

    這篇文章主要給大家介紹了關(guān)于Android開發(fā)之如何自定義數(shù)字鍵盤的相關(guān)資料,本文語言是基于kotlin實(shí)現(xiàn)的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-09-09
  • android遞歸壓縮上傳多張圖片到七牛的實(shí)例代碼

    android遞歸壓縮上傳多張圖片到七牛的實(shí)例代碼

    本篇文章主要介紹了android遞歸壓縮上傳多張圖片到七牛的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android簡潔的下拉放大刷新效果示例

    Android簡潔的下拉放大刷新效果示例

    本篇文章主要介紹了Android簡潔的下拉放大刷新效果示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10

最新評(píng)論

屏山县| 海晏县| 舞阳县| 松江区| 和硕县| 巴东县| 平定县| 淅川县| 尼木县| 佛教| 锡林郭勒盟| 巨野县| 沁水县| 汝南县| 边坝县| 兴化市| 舒城县| 应城市| 铜梁县| 松滋市| 龙川县| 明光市| 文水县| 中江县| 广饶县| 寿阳县| 巴塘县| 驻马店市| 滨海县| 延寿县| 迁安市| 华蓥市| 绥化市| 修文县| 多伦县| 新安县| 望都县| 上犹县| 卢氏县| 万源市| 邻水|