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存儲,拔掉數據線,打開手機-設置-應用-全部-圖庫-清除數據
打開第三方安全軟體-自啟管家-系統組件-圖庫,如果圖庫的狀態是禁止自啟,請取消圖庫的禁止自啟狀態,恢復圖庫的自啟行為
重啟手機,圖庫的內容就可以正常顯示了