2

我正在尝试将图像文件从我的 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: Unable to create new file 2) 粘贴时只粘贴 URI 本身,而不是图像(即使在兼容的应用程序中)

我没有看到任何人在 android 上粘贴图像的例子,我已经在谷歌和堆栈溢出上广泛搜索了答案。

有人能帮忙吗?我真的很感谢有人在这里提供帮助。

PS:如果不可能做到这一点,我也想知道,以免浪费更多时间。

谢谢!

4

3 回答 3

1

我有一个选择。使用应用程序 SwiftKey 作为您在 Android 上的键盘(它适用于 Android 10)。它允许您访问您的照片库,因此您需要 1)下载图像 2)打开您正在使用的任何应用程序并想要粘贴图像 3)使用您的 SwiftKey 键盘,单击“+”信号,然后“pin”符号(应该在第一行)。4) 最后,单击“新建”,它将访问您的照片以在任何地方插入 IN-LINE。

我知道这不是最好的解决方案,而在 iOS 上,您只需点击复制和粘贴即可。但这是唯一对我有用的解决方案。自己试试。我希望这会有所帮助:)

于 2019-09-28T23:08:49.907 回答
0

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

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

这取决于您粘贴的位置是否可以处理此uri。

于 2013-10-30T13:26:48.030 回答
-1

您不能将其复制到剪贴板,因为它不可能;但是您可以通过将其复制到 sdcard 来做到这一点,然后从您想要的任何地方访问它;

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

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);
  }
}
于 2013-10-30T13:40:27.087 回答