【问题标题】:Add text into Image and share it as Image将文本添加到图像中并作为图像共享
【发布时间】:2022-02-07 20:37:50
【问题描述】:

我需要一些帮助。 我想在我的应用程序中添加一个新功能。我有一个显示一些文本的页面,并且我有一个共享按钮。现在用户可以与任何人共享此文本,所以我想要的是用户可以将此文本共享为图像,所以我不会像文本一样共享此文本,而是添加此新功能以也可以将其共享为图像.

现在我已经创建了包含要共享的文本的图像, 我需要做的是,当用户单击共享按钮时,应该将此文本发送到图像 a 放入其中“它将是空的,文本就像在上面绘图一样。文本添加后到图像,用户可以将这个图像与文本一起发送。

我在 Google 上进行了一些搜索,发现有人说您可以将 Textview 添加到 ImageView 并使用 Intent 发送此文本,然后在 Textview 中显示文本然后共享它。但是当然,这不会起作用,因为文本实际上并没有添加到图像中,它只是一个视图,所以图像将共享,因为它是“空的”。

关于如何制作这样的东西有什么建议吗?

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    将您的图像和文本包装到相对布局或约束布局或框架布局中,截取该相对或约束或框架布局的屏幕截图,并分享它,使用This Library 截取您的视图的屏幕截图。

    这里有一些代码给你

    Bitmap bitmap = ScreenShott.getInstance().takeScreenShotOfJustView(yourview);
    File sharedFile = FileUtility.shareImageFile(bitmap);
    if (sharedFile != null) {
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName().concat(".provider"), sharedFile);
    
        Intent intent = new ShareCompat.IntentBuilder(context)
           .setType(context.getContentResolver().getType(uri))
           .setStream(uri)
           .getIntent();
           intent.setAction(Intent.ACTION_SEND);
           intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
           intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
           startActivity(Intent.createChooser(intent, "share"));
    }
    

    FileUtility shareImageFile 方法

        public static File shareImageFile(Bitmap bitmap) {
        String rootDirectory = ResourceProvider.get().getContext().getExternalCacheDir() + File.separator + Environment.DIRECTORY_PICTURES + File.separator;
        File rootFile = new File(rootDirectory);
        if (!rootFile.exists()) {
            boolean make = rootFile.mkdirs();
            Log.d(TAG, "shareImageFileMakeStatus: " + make);
        }
    
        String imagePath = rootDirectory.concat("image_").concat(currentDate()).concat(".png");
        try {
            FileOutputStream fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            return new File(imagePath);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    我使用外部 Cashe 目录,因为不需要从用户那里获得此路径的存储权限。

    您必须在 res 中添加 xml 文件夹并为文件提供程序创建 .xml 文件的文件提供程序
    res/xml/provide_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
        <paths>
            <external-path
                name="external_files"
                path="." />
        </paths>
    

    在您的 manifest.xml 中添加此提供程序

            <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths" />
            </provider>
    

    【讨论】:

      【解决方案2】:

      您可以使用 DrawingCache。将您的图像和文本放入 FrameLayout/RelativeLayout/etc 等包装器中,并使用其绘图缓存将其保存为位图;

      View view = findViewById(R.id.flWrapper);
      view.setDrawingCacheEnabled(true);
      Bitmap bitmap = Bitmap.createBitmap(flUser.getDrawingCache());
      view.setDrawingCacheEnabled(false);
      
      // if you want to save it to a file
      File image = new File("/path/to/your/file.jpg");
      try (FileOutputStream outputStream = new FileOutputStream(image)) {
          
          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
          outputStream.flush();
      
      } catch (FileNotFoundException e) {
          e.printStackTrace();
          // handle it
      } catch (IOException e) {
          e.printStackTrace();
          // handle it
      }
      
      // or share the bitmap as you like
      

      【讨论】:

        【解决方案3】:

        这有 3 个步骤。

        1. 创建要发送的视图

        2. 将视图转换为位图

        3. 将其保存在您的应用中后通过意图发送或将其用于您的图像视图

        4. 创建您想要发送的视图

        因此,创建一个约束布局,其中包含您希望通过 `intent.getExtra("text") 从上一个活动中提供的文本填充的文本视图

        将图像和其他数据放在您要发送的 *相同约束布局中。一旦您对视图/视图组(即约束布局)的外观感到满意。我们已准备好将其转换为位图

        2.将视图组转换为位图

        v.measure(MeasureSpec.makeMeasureSpec(v.getLayoutParams().width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, MeasureSpec.EXACTLY));
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
        Canvas c = new Canvas(b);
        v.draw(c);
        

        其中 v 是要转换为位图的视图组

        Viewgroup to Bitmap Stackoverflow

        3.将该位图作为意图发送

         String pathofBmp = Images.Media.insertImage(getContentResolver(), bitmap,"title", null);
            Uri bmpUri = Uri.parse(pathofBmp);
            final Intent emailIntent1 = new Intent(     android.content.Intent.ACTION_SEND);
            emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
            emailIntent1.setType("image/png");
        

        对于 imageview,它很简单 imageview.setSrc(位图)

        Share Bitmap via Android Stackoverllow answer

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多