Android – 将图像复制到剪贴板,任何人都有这个工作?

我正在尝试将图像文件从我的apk复制到剪贴板。

以下是我如何接近它(粗略地说,我在本地使用的内容提供商超出了问题的范围。

ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ContentValues values = new ContentValues(2); values.put(MediaStore.Images.Media.MIME_TYPE, "Image/jpg"); values.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath()); ContentResolver theContent = getContentResolver(); Uri imageUri = theContent.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri); mClipboard.setPrimaryClip(theClip); 

使用此代码可能会发生两件事:

1)java.lang.IllegalStateException:无法创建新文件2)粘贴时只粘贴URI本身,而不是图像(即使在兼容的应用程序中)

我没有看到任何人在Android工作中获得图像粘贴的任何例子,我已经广泛搜索了答案,无论是谷歌还是堆栈溢出。

有人能帮忙吗? 我真的很感激有人帮助。

PS:如果这是不可能的话,我也想知道,为了节省浪费时间。

谢谢!

没有迹象表明Android支持此类function。

行为是正确的,uri是复制的数据而不是位图。

这取决于你粘贴的地方是否可以处理这个uri。

你无法将其复制到剪贴板,因为它是不可能的; 但你可以通过将其复制到SD卡然后从你想要的每个地方访问它来做到这一点;

这里有一些代码可以帮助我很多,也可以帮助你:

 Context Context = getApplicationContext(); String DestinationFile = "the place that you want copy image there like sdcard/..."; if (!new File(DestinationFile).exists()) { try { CopyFromAssetsToStorage(Context, "the pictures name in assets folder of your project", DestinationFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void CopyFromAssetsToStorage(Context Context, String SourceFile, String DestinationFile) throws IOException { InputStream IS = Context.getAssets().open(SourceFile); OutputStream OS = new FileOutputStream(DestinationFile); CopyStream(IS, OS); OS.flush(); OS.close(); IS.close(); } private void CopyStream(InputStream Input, OutputStream Output) throws IOException { byte[] buffer = new byte[5120]; int length = Input.read(buffer); while (length > 0) { Output.write(buffer, 0, length); length = Input.read(buffer); } }