How to copy image to clipboardManager on Android, example?

让人想犯罪 __ 提交于 2020-01-03 11:32:43

问题


I want to copy an image stored in resources folder to clipboard manager to later be pasted on another app, like mail,whatapp or chat. i have researcher severals links some mention this can be done making an uri to a file.

This is the best i got, can somene point me to a working example of this.

File imageFile = new File("file:///android_asset/coco_001.png");
ContentValues values = new ContentValues(2);

values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.DATA, imageFile.getAbsolutePath());
ContentResolver theContent = getContentResolver();
Uri  imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
ClipData theClip = ClipData.newUri(getContentResolver(),"Image", imageUri);

回答1:


try to add these two lines of code at the end of your code.

android.content.ClipboardManager clipboard = (android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);             
clipboard.setPrimaryClip(theClip);


来源:https://stackoverflow.com/questions/13599162/how-to-copy-image-to-clipboardmanager-on-android-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!