Android 使用Palette获取Gallery图片主色调,androidstudio开发app

本文介绍了在Android中如何使用Palette获取Gallery选择图片的主色调,并详细讲解了在API 23及以上版本中如何动态申请外部存储读写权限。通过示例代码展示了从Gallery获取图片路径、尺寸,并转换为Bitmap的过程。
摘要由CSDN通过智能技术生成

}

读取Gallery图片

其中需要注意的一点,在API 23以上设备上需要动态申请外部存储读写权限,否则在BitmapFactory.decodeFile会返回null。

这里写图片描述

private void pickImageAndPalette() {

int permissionWrite = ActivityCompat.checkSelfPermission(MainActivity.this,

Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permissionWrite != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this,

PERMISSION_EXTERNAL_STORAGE, REQUEST_EXTERNAL_STORAGE);

} else {

Intent intentPick = new Intent(Intent.ACTION_PICK,

android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(intentPick, REQUEST_PICK_IMAGE);

}

}

在onActivityResult获取Gallery传回的路径和原始尺寸,这里先不压缩图片质量,直接使用图片的原始尺寸。但是需要考虑MediaStore获取不到尺寸的情况:

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_PICK_IMAGE && resultCode == RESULT_OK && null != data) {

Uri uriSelectedImage = data.getData();

String[] filePathColumn = {MediaStore.Images.Media.DATA,

MediaStore.Images.Media.WIDTH, MediaStore.Images.Media.HEIGHT};

Cursor cursor = getContentResolver().query(uriSelectedImage,

filePathColumn, null, null, null);

cursor.moveToFirst();

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

int width = cursor.getInt(cursor.getColumnIndex(filePathColumn[1]));

int height = cursor.getInt(cursor.getColumnIndex(filePathColumn[2]));

cursor.close();

if (width > 0 && height > 0) {

} else {

width = 800;

height = 800;

}

bitmap = pathToBitmap(path, width, height); // path:/storage/emulated/0/DCIM/test.png

if (bitmap != null) {

imageTest.setImageBitmap(bitmap);

paletteBitmap(pathToBitmap(path, width, height));

} else

HintUtil.showToast(context, “bitmap is NULL”);

}

}

从路径获取Bitmap:

/**

  • 从图片路径获取Bitmap

  • @param path

  • @param wantWidth

  • @param wantHeight

  • @return

*/

public Bitmap pathToBitmap(String path, int wantWidth, int wantHeight) {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true; // 设置为ture只获取图片大小

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

BitmapFactory.decodeFile(path, options); // 返回为空

int realWidth = options.outWidth;

int readlHeight = options.outHeight;

float scaleWidth = 0.f, scaleHeight = 0.f;

if (realWidth > wantWidth || readlHeight > wantHeight) { // 缩放

scaleWidth = ((float) realWidth) / wantWidth;

scaleHeight = ((float) readlHeight) / wantHeight;

}

options.inJustDecodeBounds = false;

float scale = Math.max(scaleWidth, scaleHeight);

options.inSampleSize = (int) scale;

WeakReference weak = new WeakReference<>(BitmapFactory.decodeFile(path, options));

return Bitmap.createScaledBitmap(weak.get(), wantWidth, wantHeight, true);

}

试下图库图片:

这里写图片描述
没问题,大功告成!
代码下载:Android 使用Palette获取Gallery图片主色调

最后

说一千道一万,不如自己去行动。要想在移动互联网的下半场是自己占有一席之地,那就得从现在开始,从今天开始,马上严格要求自己,既重视业务实现能力,也重视基础和原理。基础夯实好了,高楼才能够平地而起,稳如泰山。

最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2020-2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节

还有 高级架构技术进阶脑图、Android开发面试专题资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

一线互联网面试专题

379页的Android进阶知识大全

379页的Android进阶知识大全

点击:

Android架构视频+BAT面试专题PDF+学习笔记​》

即可免费获取~

tps://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)​》**

即可免费获取~

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值