场景:如标题所述,需要把图片复制到剪贴板,在另外一个程序里面(如word),通过剪贴板粘贴的方式来获取图片。
实现流程:
1、获取ClipboardManager对象 ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
2、图片使用自己存在的替换 Bitmap bitmap = CommonUtils.convertText2Bitmap(result);
3、保存图片到文件地址 File targetFile = new File(context.getCacheDir(), "write.jpg"); boolean saveResult = ImageUtils.save(bitmap, targetFile, Bitmap.CompressFormat.JPEG);
4、把uri放到剪贴板里面,这个uri哪里来的,请看第5步的android:authorities节点的值 String uri = "content://com.android.demo.FileProvider/test"; Uri copyUri = Uri.parse(uri); ClipData clip = ClipData.newUri(context.getContentResolver(), "URI", copyUri); clipboardmanager.setPrimaryClip(clip);
5、定义一个provider,在AndroidManifest.xml声明如下:
6、FileProvider的代码实现如下:
class FileProvider : ContentProvider() {
private lateinit var basePath: String
override fun onCreate(): Boolean {
basePath = context!!.cacheDir!!.path
return true
}
override fun query(
uri: Uri,
projection: Array?,
selection: String?,
selectionArgs: Array?,
sortOrder: String?
): Cursor? {
TODO("Not yet implemented")
}
override fun getType(uri: Uri): String? {
return "image/jpeg"
}
override fun insert(uri: Uri, values: ContentValues?): Uri? {
TODO("Not yet implemented")
}
override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int {
TODO("Not yet implemented")
}
override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array?
): Int {
TODO("Not yet implemented")
}
override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
var imode = 0
if (mode.contains("w")) imode = imode or ParcelFileDescriptor.MODE_WRITE_ONLY
if (mode.contains("r")) imode = imode or ParcelFileDescriptor.MODE_READ_ONLY
if (mode.contains("+")) imode = imode or ParcelFileDescriptor.MODE_APPEND
val pseg = uri.pathSegments
// if (pseg.size < 3) return null
try {
// val filePath: File = filePathFromRecord(pseg[2], pseg[1])
val filePath: File = getFile(basePath, uri, mode)
return ParcelFileDescriptor.open(filePath, imode)
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
return null
}
companion object {
fun getFile(basePath: String, uri: Uri, mode: String): File {
return File(basePath, "write.jpg")
}
}
}
7、从剪贴版粘贴的实现如下:
public void clickButton2(ImageView imageView) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ContentResolver cr = getContentResolver();
ClipData clip = clipboard.getPrimaryClip();
if (clip == null) {
Log.d(TAG, "null");
return;
}
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
if (item == null) {
return;
}
String pasteData = (String) item.getText();
if (ObjectUtils.isNotEmpty(pasteData)) {
Log.d(TAG, pasteData);
return;
}
Uri pasteUri = item.getUri();
if (pasteUri != null) {
Log.d(TAG, pasteUri.toString());
String uriMimeType = cr.getType(pasteUri);
if (uriMimeType != null) {
if (uriMimeType.contains("image")) {
Log.d(TAG, "find a image in clipboard");
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr, pasteUri);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}