将RelativeLayout保存为位图

问题描述 投票:0回答:1

我正在尝试将RelativeLayout另存为bitmap并将其插入到图库中,下面的代码引发此异常:

java.lang.NullPointerException
        at java.io.File.<init>(File.java:282)
        at maa.Fragements.ColorPaletteFragment.galleryAddPic(ColorPaletteFragment.java:344) 

因为imagePath方法的参数galleryAddPic

将图片插入图库:

 private void galleryAddPic(String imagePath) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imagePath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        getActivity().sendBroadcast(mediaScanIntent);
    }

保存图像:

@SuppressLint("StaticFieldLeak")
    public class saveViewAsBitmap extends AsyncTask<RelativeLayout, String, String> {
        @SuppressLint("SetTextI18n")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            txt.setText("Saving image to gallery ...");
        }

        @Override
        protected String doInBackground(RelativeLayout... relatives) {
            String savedImagePath = null;
            String imageFileName = "inColor" + System.currentTimeMillis() + ".png";
            Bitmap image = getBitmapFromView(relatives[0]);
            File storageDir = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + "/inColor Folder");
            boolean success = true;
            if (!storageDir.exists()) {
                success = storageDir.mkdirs();
            }
            if (success) {
                File imageFile = new File(storageDir, imageFileName);
                savedImagePath = imageFile.getAbsolutePath();
                try {
                    OutputStream fOut = new FileOutputStream(imageFile);
                    image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialogLoading.dismiss();
            }
            return savedImagePath;
        }

        @Override
        protected void onPostExecute(String path) {
            super.onPostExecute(path);
            galleryAddPic(path);
        }
    } 

有人可以帮助我解决此问题吗?

android android-bitmap android-relativelayout
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.