【问题标题】:Exporting Subview of Fragment from another Fragment as Png从另一个片段导出片段的子视图为Png
【发布时间】:2023-03-20 14:52:01
【问题描述】:

如何将另一个片段的片段子视图导出为 Png?

上下文

我正在创建一个应用程序,它允许用户创建个性化的简历。用户可以提交有关他的工作经验和技能的信息。因此,用户能够将他的结果导出为 png 并将其保存到设备中。我的问题针对应用程序的导出功能。

到目前为止我得到了什么

我试图结合网站上的几个答案来获得结果,但不幸的是我的代码到目前为止还不起作用。

public void export(Context context) throws FileNotFoundException {

    View  exportView = getLayoutInflater(getArguments()).inflate(R.layout.fragment_form, null, false);
    RelativeLayout subView = (RelativeLayout) exportView.findViewById(R.id.fragment_form_container_root);

    try {

        subView.setDrawingCacheEnabled(true);
        subView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        Bitmap b = Bitmap.createBitmap(subView.getDrawingCache(), 0, 0, subView.getMeasuredWidth(), subView.getMeasuredHeight());

        File cachePath = new File(context.getCacheDir(), "images");
        cachePath.mkdirs(); // don't forget to make the directory
        FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
        b.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.close();


    } catch (Exception e) {
        e.printStackTrace();
    }


    File imagePath = new File(context.getCacheDir(), "images");
    File newFile = new File(imagePath, "image.png");
    Uri contentUri = FileProvider.getUriForFile(context, "com.example.lukas.masterthesis.fileprovider", newFile);

    if (contentUri != null) {

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
        shareIntent.setDataAndType(contentUri, getActivity().getContentResolver().getType(contentUri));
        shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
        startActivity(Intent.createChooser(shareIntent, "Choose an app"));

    }

}

在点击事件之后在片段 A 中调用导出功能。我正在尝试获取 Fragment B 的所需子视图(似乎可行),但创建位图总是会导致 null 对象。因此程序失败。

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null 

老实说,我什至不确定这是做这种事情的正确方法,因为我还没有找到适合我尝试的最佳实践解决方案。如果没有,有人知道更好的方法吗?

如果它是一个“好的”解决方案,是否有人知道如何让我的代码正常工作或有一些建议可以为我指明正确的方向?

提前致谢。

【问题讨论】:

    标签: android xml android-fragments export png


    【解决方案1】:

    尝试使用

    try using bitmap = Bitmap.createBitmap(subView.getDrawingCache());
    

    而不是

    Bitmap b = subView.getDrawingCache();
    

    【讨论】:

    • 没有效果,只有错误信息改变:java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
    【解决方案2】:

    只是想提供一个答案。

    我基本上是在正确的轨道上。最终将我的视图分成更小的部分,因为绘图缓存可能太小而无法一次处理整个事情。它可能会对遇到类似问题的人有所帮助。

    代码的第一部分将每个部分绘制到生成的位图中。第二部分允许用户导出它(例如到 Dropbox)。

    那么你来了

    public void export() throws FileNotFoundException {
    
        View exportView =  rootView.findViewById(R.id.fragment_form_container_root); 
        View view_title = rootView.findViewById(R.id.fragment_form_container_title);
        View view_basicInformation = rootView.findViewById(R.id.fragment_form_container_basicinformation);
        View view_experience_skill = rootView.findViewById(R.id.fragment_form_container_experience_skill);
        View view_connect = rootView.findViewById(R.id.fragment_form_container_connect);
        View view_footer = rootView.findViewById(R.id.fragment_form_container_footer);
    
    
        try {
    
            view_title.setDrawingCacheEnabled(true);
            view_title.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap b0 =  Bitmap.createBitmap(view_title.getDrawingCache());
            view_title.setDrawingCacheEnabled(false);
    
            view_basicInformation.setDrawingCacheEnabled(true);
            view_basicInformation.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap b1 =  Bitmap.createBitmap(view_basicInformation.getDrawingCache());
            view_basicInformation.setDrawingCacheEnabled(false);
    
    
            view_experience_skill.setDrawingCacheEnabled(true);
            view_experience_skill.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap b2 =  Bitmap.createBitmap(view_experience_skill.getDrawingCache());
            view_experience_skill.setDrawingCacheEnabled(false);
    
            view_connect.setDrawingCacheEnabled(true);
            view_connect.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap b3 =  Bitmap.createBitmap(view_connect.getDrawingCache());
            view_connect.setDrawingCacheEnabled(false);
    
            view_footer.setDrawingCacheEnabled(true);
            view_footer.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap b4 =  Bitmap.createBitmap(view_footer.getDrawingCache());
            view_footer.setDrawingCacheEnabled(false);
    
    
            Bitmap result = Bitmap.createBitmap(exportView.getWidth(), exportView.getHeight(), Bitmap.Config.ARGB_8888);
    
            Canvas canvas = new Canvas(result);
            //canvas.setDensity(300);
            Paint paint = new Paint();
            paint.setFlags(Paint.FILTER_BITMAP_FLAG);
            paint.setFlags(Paint.ANTI_ALIAS_FLAG);           
    
            canvas.drawBitmap(b0, 0, 0, paint);
            canvas.drawBitmap(b1, 0, b0.getHeight(), paint);
            canvas.drawBitmap(b2, 0, b0.getHeight() + b1.getHeight(), paint);
            canvas.drawBitmap(b3, 0, b0.getHeight() + b1.getHeight() + b2.getHeight(), paint);
            canvas.drawBitmap(b4, 0, b0.getHeight() + b1.getHeight() + b2.getHeight() + b3.getHeight(), paint);
    
            File cachePath = new File(string_CachePath, "images"); // -> string_CachePath == context.getCacheDir(); 
            cachePath.mkdirs(); // don't forget to make the directory
            FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
            result.compress(Bitmap.CompressFormat.PNG, 100, stream);
            stream.close();
    
            File newFile = new File(cachePath.getPath(), "image.png");
    
            Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.example.lukas.masterthesis.fileprovider", newFile);
    
            if (contentUri != null) {
    
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
                shareIntent.setDataAndType(contentUri, getActivity().getContentResolver().getType(contentUri));
                shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
                startActivity(Intent.createChooser(shareIntent, "Choose an app"));
    
            }
    
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 1970-01-01
      • 2013-12-19
      • 2017-03-25
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多