android系统图库
1. 安卓从手机相册获取照片作为头像缓存地址
* 1、实现原理:用户打开相册或相机选择相片后,相片经过压缩并设置在控件上,图片在本地sd卡存一份(如果有的话,没大桐有则内部存储,所以还
* 需要判断用户是否挂载了sd卡),然后在服务器上存储一份该图片,当下次再次启桐则动应用时,会默认去sd卡加载该图片,如果本地没有,再会去联网请求
* 2、使用了picasso框架以及自定义BitmapUtils工具类
* 3、记得加上相关权局仿棚限
* <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv;//要设置的头像
private Button btn_photo;//调用相册按钮
private Button btn_camera;//调用相机按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
btn_photo = (Button) findViewById(R.id.btn_photo);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_photo.setOnClickListener(this);
btn_camera.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_photo://打开系统相册
Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,100);
break;
case R.id.btn_camera://打开系统相机
Intent intent2=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent2,200);
break;
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100&&resultCode==RESULT_OK&&data!=null){//系统相册
Uri imageData = data.getData();
String path=getPath(imageData);
Bitmap bitmap = BitmapFactory.decodeFile(path);
Bitmap bitmap1 = BitmapUtils.zoom(bitmap, iv.getWidth(), iv.getHeight());
Bitmap bitmap2 = BitmapUtils.circleBitmap(bitmap1);
//加载显示
iv.setImageBitmap(bitmap2);
//bitmap图片上传到服务器......
//bitmap图片保存到本地
saveImage(bitmap2);
}else if(requestCode==200&&resultCode==RESULT_OK&&data!=null){//系统相机
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
BitmapUtils.zoom(bitmap,iv.getWidth(),iv.getHeight());
bitmap=BitmapUtils.circleBitmap(bitmap);
//加载显示
iv.setImageBitmap(bitmap);
//bitmap图片上传到服务器......
//bitmap图片保存到本地
saveImage(bitmap);
}
}
/**
* 数据的存储。(5种)
* Bimap:内存层面的图片对象。
*
* 存储--->内存:
* BitmapFactory.decodeFile(String filePath);
* BitmapFactory.decodeStream(InputStream is);
* 内存--->存储:
* bitmap.compress(Bitmap.CompressFormat.PNG,100,OutputStream os);
*/
private void saveImage(Bitmap bitmap) {
File filesDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断sd卡是否挂载
//路径1:storage/sdcard/Android/data/包名/files
filesDir = this.getExternalFilesDir("");
}else{//手机内部存储
//路径:data/data/包名/files
filesDir = this.getFilesDir();
}
FileOutputStream fos = null;
try {
File file = new File(filesDir,"icon.png");
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100,fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//如果本地有,就不需要再去联网去请求
private boolean readImage() {
File filesDir;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断sd卡是否挂载
//路径1:storage/sdcard/Android/data/包名/files
filesDir = getExternalFilesDir("");
}else{//手机内部存储
//路径:data/data/包名/files
filesDir = getFilesDir();
}
File file = new File(filesDir,"icon.png");
if(file.exists()){
//存储--->内存
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
iv.setImageBitmap(bitmap);
return true;
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private String getPath(Uri uri) {
int sdkVersion = Build.VERSION.SDK_INT;
//高于4.4.2的版本
if (sdkVersion >= 19) {
Log.e("TAG", "uri auth: " + uri.getAuthority());
if (isExternalStorageDocument(uri)) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(this, contentUri, null, null);
} else if (isMediaDocument(uri)) {
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(this, contentUri, selection, selectionArgs);
} else if (isMedia(uri)) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = this.managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
return actualimagecursor.getString(actual_image_column_index);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(this, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* uri路径查询字段
*
* @param context
* @param uri
* @param selection
* @param selectionArgs
* @return
*/
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 index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isMedia(Uri uri) {
return "media".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
/**
* 判断本地是否有该图片,没有则去联网请求
* */
@Override
protected void onResume() {
super.onResume();
if(readImage()){
return;
}
}
}
//BitmapUtils工具类public class BitmapUtils { /**
* 该方法用于将图片进行圆形处理
* */ public static Bitmap circleBitmap(Bitmap source){ //默认只对宽进行处理 int width=source.getWidth(); Bitmap bitmap=Bitmap.createBitmap(width,width,Bitmap.Config.ARGB_8888); Canvas canvas=new Canvas(bitmap); Paint paint=new Paint(); //设置抗锯齿 paint.setAntiAlias(true); canvas.drawCircle(width/2,width/2,width/2,paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source,0,0,paint); return bitmap; } /** * 该方法用于图片压缩处理,注意width、height参数的类型必须是float * */ public static Bitmap zoom(Bitmap source,float width,float height){ Matrix matrix=new Matrix(); //图片进行压缩处理 matrix.postScale(width/source.getWidth(),height/source.getHeight()); Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false); return bitmap; }}
以上所述是小编给大家介绍的Android实现调用系统图库与相机设置头像并保存在本地及服务器 ,希望对大家有所帮助
2. android 图库刷新不了怎么办 手机内存卡里面新放进的图片,图库里面显示不出来,怀疑是刷新不了,怎么办
Android手机图库的图片不显示,刷新失败等错误,多数情况下都是图库或者相册这个应用由于某些原因导致出错了,导致无法预览,刷新。
以下是解决方法:
打开手机的图库,可以看见图库不显示任何图片,提示没有图片/视频,只有一个大大的相机图标
进入文件管理-SD卡-images以及其他存放有图片的文件夹可以发现手机上的图片都没有损坏,可正常显示
使用手机数据线将手机和电脑连接,选择打开USB存储
勾选显示隐藏文件夹,找到一个叫做.nomedia的文件夹,删除,部分第三方刷机包ROM可能内置这个文件夹,以此来保护用户的隐私不显示在手机上
关闭USB存储,拔掉数据线,打开手机-设置-应用-全部-图库-清除数据
打开第三方安全软件-自启管家-系统组件-图库,如果图库的状态是禁止自启,请取消图库的禁止自启状态,恢复图库的自启行为
重启手机,图库的内容就可以正常显示了