当前位置:   article > 正文

Android ClipboardManager 复制图片到剪贴板+从剪贴板粘贴图片的实现(亲测有效)

Android ClipboardManager 复制图片到剪贴板+从剪贴板粘贴图片的实现(亲测有效)
场景:如标题所述,需要把图片复制到剪贴板,在另外一个程序里面(如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声明如下:

<provider
    android:name=".component.provider.FileProvider"
    android:authorities="com.android.demo.FileProvider"
    android:exported="true"
    android:multiprocess="true" />

6、FileProvider的代码实现如下:

  1. class FileProvider : ContentProvider() {
  2. private lateinit var basePath: String
  3. override fun onCreate(): Boolean {
  4. basePath = context!!.cacheDir!!.path
  5. return true
  6. }
  7. override fun query(
  8. uri: Uri,
  9. projection: Array<out String>?,
  10. selection: String?,
  11. selectionArgs: Array<out String>?,
  12. sortOrder: String?
  13. ): Cursor? {
  14. TODO("Not yet implemented")
  15. }
  16. override fun getType(uri: Uri): String? {
  17. return "image/jpeg"
  18. }
  19. override fun insert(uri: Uri, values: ContentValues?): Uri? {
  20. TODO("Not yet implemented")
  21. }
  22. override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
  23. TODO("Not yet implemented")
  24. }
  25. override fun update(
  26. uri: Uri,
  27. values: ContentValues?,
  28. selection: String?,
  29. selectionArgs: Array<out String>?
  30. ): Int {
  31. TODO("Not yet implemented")
  32. }
  33. override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
  34. var imode = 0
  35. if (mode.contains("w")) imode = imode or ParcelFileDescriptor.MODE_WRITE_ONLY
  36. if (mode.contains("r")) imode = imode or ParcelFileDescriptor.MODE_READ_ONLY
  37. if (mode.contains("+")) imode = imode or ParcelFileDescriptor.MODE_APPEND
  38. val pseg = uri.pathSegments
  39. // if (pseg.size < 3) return null
  40. try {
  41. // val filePath: File = filePathFromRecord(pseg[2], pseg[1])
  42. val filePath: File = getFile(basePath, uri, mode)
  43. return ParcelFileDescriptor.open(filePath, imode)
  44. } catch (e: FileNotFoundException) {
  45. e.printStackTrace()
  46. }
  47. return null
  48. }
  49. companion object {
  50. fun getFile(basePath: String, uri: Uri, mode: String): File {
  51. return File(basePath, "write.jpg")
  52. }
  53. }
  54. }

7、从剪贴版粘贴的实现如下:

  1. public void clickButton2(ImageView imageView) {
  2. ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
  3. ContentResolver cr = getContentResolver();
  4. ClipData clip = clipboard.getPrimaryClip();
  5. if (clip == null) {
  6. Log.d(TAG, "null");
  7. return;
  8. }
  9. ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
  10. if (item == null) {
  11. return;
  12. }
  13. String pasteData = (String) item.getText();
  14. if (ObjectUtils.isNotEmpty(pasteData)) {
  15. Log.d(TAG, pasteData);
  16. return;
  17. }
  18. Uri pasteUri = item.getUri();
  19. if (pasteUri != null) {
  20. Log.d(TAG, pasteUri.toString());
  21. String uriMimeType = cr.getType(pasteUri);
  22. if (uriMimeType != null) {
  23. if (uriMimeType.contains("image")) {
  24. Log.d(TAG, "find a image in clipboard");
  25. try {
  26. Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr, pasteUri);
  27. imageView.setImageBitmap(bitmap);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }
  34. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/558394
推荐阅读
相关标签
  

闽ICP备14008679号