将片段的子视图从另一个片段导出为Png

时间:2015-07-25 12:55:12

标签: android xml android-fragments export png

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

上下文

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

enter image description here

到目前为止我得到了什么

我尝试将网站上的几个答案结合起来得到一个结果,但不幸的是,我所拥有的代码到目前为止还无效。

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"));

    }

}

在单击事件后,在Fragment A中调用export functionallity。我试图得到片段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 

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

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

提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试使用

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

而不是

Bitmap b = subView.getDrawingCache();

答案 1 :(得分:0)

只想提供答案。

我基本上走在了正确的轨道上。结束了将我的视图拆分成较小的部分,因为绘制缓存可能很小,可以立即处理整个事情。它可能会帮助遇到类似问题的人。

代码的第一部分将每个部分绘制到结果位图中。第二部分允许用户导出它(例如导出到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();
    }
}