0

I'm developing an application that save the drawing cache as image.png. I want to save every every 2 seconds the image and then send it to my server.

public static void getImagen(){
    mView.setDrawingCacheEnabled(true);
    mView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    mBitmap = mView.getDrawingCache();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File file = new File(path+"/signature.png");
    FileOutputStream ostream;
    try {
        file.createNewFile();
        ostream = new FileOutputStream(file);
        mBitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.flush();
        ostream.close();
        Log.w("LOGCAT", "La imagen se guardo con exito");

        //Toast.makeText(getActivity(), "image saved", 5000).show();
    } catch (Exception e) {
        e.printStackTrace();
        Log.w("LOGCAT", "No se pudo guardar la imagen...");

        //Toast.makeText(getActivity(), "error", 5000).show();
    }
    // mView.setDrawingCacheEnabled(false);
}

And then I call that method here(Frament's onCreate):

t = new Thread(){
        @Override
        public void run() {
            super.run();
            while(flag){

                Log.w("THREAD_LOG", "guardar");
                try {
                    getImagen();
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.w("THREAD_LOG", "Error: "+e.toString());
                }

            }
        }
    };

    t.start();

And I got a null pointer exception at this line mView.setDrawingCacheEnabled(true);

My code of fragment view is the next:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_b, container, false);
    referencias();


    return v;
}

1 Answers1

0

You can get the decor view and save it instead of having to use your own view. Also drawing cache can be inconsistent at times, you should just use draw instead (since its every 2 seconds, the performance difference might be very small).

You can get the rootview using:

ViewGroup mRootView = (ViewGroup) activity.getWindow().getDecorView();

You can then getDrawingCache, and use Bitmap.Compress on it.

Hiemanshu Sharma
  • 7,702
  • 1
  • 16
  • 13