图片保存后通知系统图库

因为android10的出现 所以需要判断下版本号

方法1

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {

String[] proj = {MediaStore.Images.Media.DATA};

Cursor actualimagecursor = context.getContentResolver().query(uri, proj, null, null, null);

int actualImageColumnIndex = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

actualimagecursor.moveToFirst();

String imgPath = actualimagecursor.getString(actualImageColumnIndex);

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(imgPath))));

}

方法2

String[]   filePathArray

String   filePath;

String   fileName;

String[]  filePathColumn={MediaStore.MediaColumns.DATA,MediaStore.MediaColumns.DISPLAY_NAME};

ContentResolvercontentResolver=context.getContentResolver();

Cursorcursor=contentResolver.query(contentUri, filePathColumn,null,

null,null);

cursor.moveToFirst();

filePath=cursor.getString(cursor.getColumnIndex(filePathColumn[0]));

fileName=cursor.getString(cursor.getColumnIndex(filePathColumn[1]));

cursor.close();

filePathArray=newString[]{filePath,fileName};

MediaScannerConnection.scanFile(context,newString[] {filePathArray[0]},newString[]{"image/jpeg"},newMediaScannerConnection.OnScanCompletedListener(){

@Override

publicvoidonScanCompleted(Stringpath,Uriuri) {

Log.e(TAG,"PATH:"+path);

                         }

 } );

这两种方法 都OK

你可能感兴趣的:(图片保存后通知系统图库)