summaryrefslogtreecommitdiff
path: root/LoopbackApp/app/src/main/java/org/drrickorang/loopback/LoopbackActivity.java
blob: 0f3bc37959a44c32cf670c03c6e8f073af00f0ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drrickorang.loopback;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.database.Cursor;
import android.provider.MediaStore;
import android.os.ParcelFileDescriptor;


import java.io.FileDescriptor;

import android.media.AudioManager;
//import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.TextView;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import java.io.FileOutputStream;
import java.io.File;

import android.os.Build;

public class LoopbackActivity extends Activity {
    /**
     * Member Vars
     */

    public final static String SETTINGS_OBJECT = "org.drrickorang.loopback.SETTINGS_OBJECT";

    private static final int SAVE_TO_WAVE_REQUEST = 42;
    private static final int SAVE_TO_PNG_REQUEST = 43;

    private static final int SETTINGS_ACTIVITY_REQUEST_CODE = 44;
    private static final int ABOUT_ACTIVITY_REQUEST_CODE = 45;
    LoopbackAudioThread audioThread = null;
    NativeAudioThread nativeAudioThread = null;
    private WavePlotView mWavePlotView;
    private String mCurrentTime = "IncorrectTime";  // The time the plot is acquired
    private String mFilePathWav;


    SeekBar  mBarMasterLevel; //drag the volumn
    TextView mTextInfo;
    TextView mTextViewCurrentLevel;
    TextView mTextViewEstimatedLatency;
    private double [] mWaveData;
    private Correlation mCorrelation = new Correlation();
    int mSamplingRate;

    Toast toast;

    private Handler mMessageHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what) {
                case LoopbackAudioThread.FUN_PLUG_AUDIO_THREAD_MESSAGE_REC_STARTED:
                    log("got message java rec started!!");
                    showToast("Java Recording Started");
                    refreshState();
                    break;
                case LoopbackAudioThread.FUN_PLUG_AUDIO_THREAD_MESSAGE_REC_ERROR:
                    log("got message java rec can't start!!");
                    showToast("Java Recording Error. Please try again");
                    refreshState();
                    stopAudioThread();
                    break;
                case LoopbackAudioThread.FUN_PLUG_AUDIO_THREAD_MESSAGE_REC_COMPLETE:
                    if(audioThread != null) {
                        mWaveData = audioThread.getWaveData();
                        mSamplingRate = audioThread.mSamplingRate;
                        mCorrelation.computeCorrelation(mWaveData,mSamplingRate);
                        log("got message java rec complete!!");
                        refreshPlots();
                        refreshState();
                        mCurrentTime = (String) DateFormat.format("MMddkkmmss", System.currentTimeMillis());
                        showToast("Java Recording Completed");
                        stopAudioThread();
                    }
                    break;
                case NativeAudioThread.FUN_PLUG_NATIVE_AUDIO_THREAD_MESSAGE_REC_STARTED:
                    log("got message native rec started!!");
                    showToast("Native Recording Started");
                    refreshState();
                    break;
                case NativeAudioThread.FUN_PLUG_NATIVE_AUDIO_THREAD_MESSAGE_REC_ERROR:
                    log("got message native rec can't start!!");
                    showToast("Native Recording Error. Please try again");
                    refreshState();
                    break;
                case NativeAudioThread.FUN_PLUG_NATIVE_AUDIO_THREAD_MESSAGE_REC_COMPLETE:
                case NativeAudioThread.FUN_PLUG_NATIVE_AUDIO_THREAD_MESSAGE_REC_COMPLETE_ERRORS:
                    if(nativeAudioThread != null) {
                        mWaveData = nativeAudioThread.getWaveData();
                        mSamplingRate = nativeAudioThread.mSamplingRate;
                        mCorrelation.computeCorrelation(mWaveData, mSamplingRate);
                        log("got message native rec complete!!");
                        refreshPlots();
                        refreshState();
                        if(msg.what == NativeAudioThread.FUN_PLUG_NATIVE_AUDIO_THREAD_MESSAGE_REC_COMPLETE_ERRORS) {
                            mCurrentTime = (String) DateFormat.format("MMddkkmmss", System.currentTimeMillis());
                            showToast("Native Recording Completed with ERRORS");
                        } else {
                            mCurrentTime = (String) DateFormat.format("MMddkkmmss", System.currentTimeMillis());
                            showToast("Native Recording Completed");
                        }
                        stopAudioThread();
                    }
                    break;
                default:
                    log("Got message:"+msg.what);
                    break;
            }
        }
    };


    // Thread thread;

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity. You can find it
        View view = getLayoutInflater().inflate(R.layout.main_activity, null);
        setContentView(view);

        mTextInfo = (TextView) findViewById(R.id.textInfo);
        mBarMasterLevel = (SeekBar) findViewById(R.id.BarMasterLevel);

        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mBarMasterLevel.setMax(maxVolume);

        mBarMasterLevel.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                am.setStreamVolume(AudioManager.STREAM_MUSIC,
                        progress, 0);
                refreshState();
                log("Changed stream volume to: " + progress);
            }
        });
        mWavePlotView = (WavePlotView) findViewById(R.id.viewWavePlot);

        mTextViewCurrentLevel = (TextView) findViewById(R.id.textViewCurrentLevel);

        mTextViewEstimatedLatency = (TextView) findViewById(R.id.textViewEstimatedLatency);
        refreshState();
    }

    private void stopAudioThread() {
        log("stopping audio threads");
        if (audioThread != null) {
            audioThread.isRunning = false;
            // isRunning = false;
            try {
                audioThread.finish();
                audioThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            audioThread = null;
        }
        if (nativeAudioThread != null) {
            nativeAudioThread.isRunning = false;
            // isRunning = false;
            try {
                nativeAudioThread.finish();
                nativeAudioThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            nativeAudioThread = null;
        }
        System.gc();
    }

    public void onDestroy() {
        stopAudioThread();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //web.loadUrl(stream);
        log("on resume called");

        //restartAudioSystem();
    }

    @Override
    protected void onPause () {
        super.onPause();
        //stop audio system
        stopAudioThread();
    }

    public boolean isBusy() {

        boolean busy = false;

        if( audioThread != null) {
            if(audioThread.isRunning)
                busy = true;
        }

        if( nativeAudioThread != null) {
            if(nativeAudioThread.isRunning)
                busy = true;
        }

        return busy;
     }

    private void restartAudioSystem() {

        log("restart audio system...");

        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int sessionId = 0; /* FIXME runtime test for am.generateAudioSessionId() in API 21 */

        int samplingRate = getApp().getSamplingRate();
        int playbackBuffer = getApp().getPlayBufferSizeInBytes();
        int recordBuffer = getApp().getRecordBufferSizeInBytes();
        int micSource = getApp().getMicSource();

        log(" current sampling rate: " + samplingRate);
        stopAudioThread();

        //select if java or native audio thread
        if (getApp().getAudioThreadType() == LoopbackApplication.AUDIO_THREAD_TYPE_JAVA ) {
            int micSourceMapped = getApp().mapMicSource(LoopbackApplication.AUDIO_THREAD_TYPE_JAVA ,micSource);
            audioThread = new LoopbackAudioThread();
            audioThread.setMessageHandler(mMessageHandler);
            audioThread.mSessionId = sessionId;
            audioThread.setParams(samplingRate, playbackBuffer, recordBuffer,micSourceMapped);
            audioThread.start();
        } else {
            int micSourceMapped = getApp().mapMicSource(LoopbackApplication.AUDIO_THREAD_TYPE_NATIVE ,micSource);
            nativeAudioThread = new NativeAudioThread();
            nativeAudioThread.setMessageHandler(mMessageHandler);
            nativeAudioThread.mSessionId = sessionId;
            nativeAudioThread.setParams(samplingRate, playbackBuffer, recordBuffer,micSourceMapped);
            nativeAudioThread.start();
        }
        mWavePlotView.setSamplingRate( samplingRate);


        //first refresh
        refreshState();
    }
    private void resetBufferPeriodRecord() {
        BufferPeriod.resetRecord();
    }

    /** Called when the user clicks the button */
    public void onButtonTest(View view) {
        int samplingRate = getApp().getSamplingRate();
        int playbackBuffer = getApp().getPlayBufferSizeInBytes()/getApp().BYTES_PER_FRAME;
        int recordBuffer = getApp().getRecordBufferSizeInBytes()/getApp().BYTES_PER_FRAME;
        int micSource = getApp().getMicSource();
        String micSourceName = getApp().getMicSourceString(micSource);
        int audioThreadType = getApp().getAudioThreadType();
        log("On button test sampling rate: " + samplingRate);
        log("On button test playbackBuffer: " + playbackBuffer);
        log("On button test recordBuffer: " + recordBuffer);
        log("On button test micSource Name: " + micSourceName);
        log("On button test thread type: " + audioThreadType);  //java =0, native = 1

        if( !isBusy()) {
            restartAudioSystem();
            resetBufferPeriodRecord();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (getApp().getAudioThreadType() == LoopbackApplication.AUDIO_THREAD_TYPE_JAVA) {
                if (audioThread != null) {
                    audioThread.runTest();
                }
            } else {
                if (nativeAudioThread != null) {
                    nativeAudioThread.runTest();
                }
            }

        } else {
            //please wait, or restart application.
//            Toast.makeText(getApplicationContext(), "Test in progress... please wait",
//                    Toast.LENGTH_SHORT).show();

            showToast("Test in progress... please wait");
        }

    }

    /** Called when the user clicks the button */
    public void onButtonSave(View view) {

        //create filename with date
        String date = mCurrentTime;  // the time the plot is acquired
        String micSource = getApp().getMicSourceString(getApp().getMicSource());
        String fileName = "loopback_"+/*micSource+"_"+*/date;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {


            Intent intent2 = new Intent(Intent.ACTION_CREATE_DOCUMENT);
            intent2.addCategory(Intent.CATEGORY_OPENABLE);
            intent2.setType("image/png");

            intent2.putExtra(Intent.EXTRA_TITLE, fileName + ".png"); //suggested filename
            startActivityForResult(intent2, SAVE_TO_PNG_REQUEST);

            // browser.
            Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("audio/wav");

            // sometimes ".wav" will be added automatically, sometimes not
            intent.putExtra(Intent.EXTRA_TITLE, fileName + ".wav"); //suggested filename
            startActivityForResult(intent, SAVE_TO_WAVE_REQUEST);

        } else {
            showToast("Saving Wave to: "+fileName+".wav");

            //save to a given uri... local file?
            Uri uri = Uri.parse("file://mnt/sdcard/"+fileName+".wav");

            // for some devices it cannot find the path
            String temp = getPath1(uri);
            if (temp != null) {
                File file = new File(temp);
                mFilePathWav = file.getAbsolutePath();
            } else {
                mFilePathWav = "";

            }

            saveToWavefile(uri);
            Uri uri2 = Uri.parse("file://mnt/sdcard/"+fileName+".png");
            saveScreenShot(uri2);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode,
            Intent resultData) {
        log("ActivityResult request: " + requestCode + "  result:" + resultCode);
        if (requestCode == SAVE_TO_WAVE_REQUEST && resultCode == Activity.RESULT_OK) {
            log("got SAVE TO WAV intent back!");
            Uri uri = null;
            if (resultData != null) {
                uri = resultData.getData();

                String temp = getPath1(uri);
                if (temp != null) {
                    File file = new File(temp);
                    mFilePathWav = file.getAbsolutePath();
                } else {
                    mFilePathWav = "";
                }

                saveToWavefile(uri);
            }
        } else if( requestCode == SAVE_TO_PNG_REQUEST && resultCode == Activity.RESULT_OK)  {

            log("got SAVE TO PNG intent back!");
            Uri uri = null;
            if (resultData != null) {
                uri = resultData.getData();
                saveScreenShot(uri);
            }

        } else if (requestCode == SETTINGS_ACTIVITY_REQUEST_CODE &&
                resultCode == Activity.RESULT_OK) {
            //new settings!
            log("return from settings!");
            refreshState();
        }
    }

    // method to get the file path from uri. Doesn't work for all devices
    public String getPath1(Uri uri)
    {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor1 = getContentResolver().query(uri, projection, null, null, null);
        if (cursor1 == null) {
            // cursor1.close();
            return uri.getPath();
        }

        int ColumnIndex = cursor1.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor1.moveToFirst();
        String path = cursor1.getString(ColumnIndex);
        cursor1.close();
        return path;
    }



    /** Called when the user clicks the button */
    public void onButtonZoomOutFull(View view) {

        double fullZoomOut = mWavePlotView.getMaxZoomOut();

        mWavePlotView.setZoom(fullZoomOut);
        mWavePlotView.refreshGraph();
    }

    public void onButtonZoomOut(View view) {

        double zoom = mWavePlotView.getZoom();

        zoom = 2.0 *zoom;
        mWavePlotView.setZoom(zoom);
        mWavePlotView.refreshGraph();
    }



    /** Called when the user clicks the button */
    public void onButtonZoomIn(View view) {

        double zoom = mWavePlotView.getZoom();

        zoom = zoom/2.0;
        mWavePlotView.setZoom(zoom);
        mWavePlotView.refreshGraph();
    }

/*
    public void onButtonZoomInFull(View view) {

        double minZoom = mWavePlotView.getMinZoomOut();

        mWavePlotView.setZoom(minZoom);
        mWavePlotView.refreshGraph();
    }
*/

    public void onButtonAbout(View view) {
        if(!isBusy()) {
            Intent aboutIntent = new Intent(this, AboutActivity.class);
            startActivity(aboutIntent);
        } else
            showToast("Test in progress... please wait");
    }


    public void onButtonBufferPeriod(View view) {
        if(!isBusy()) {
            HistogramView.setBufferPeriodArray(BufferPeriod.getBufferPeriodArray());
            HistogramView.setMaxBufferPeriod(BufferPeriod.getMaxBufferPeriod());

            Intent aboutIntent = new Intent(this, BufferPeriodActivity.class);
            startActivity(aboutIntent);
        } else
            showToast("Test in progress... please wait");
    }

    /** Called when the user clicks the button */
    public void onButtonSettings(View view) {

        if(!isBusy()) {
            Intent mySettingsIntent = new Intent(this, SettingsActivity.class);
            //send settings
            startActivityForResult(mySettingsIntent, SETTINGS_ACTIVITY_REQUEST_CODE);
        } else {
            showToast("Test in progress... please wait");
//            Toast.makeText(getApplicationContext(), "Test in progress... please wait",
//                    Toast.LENGTH_SHORT).show();
        }
    }

    void refreshPlots() {
        mWavePlotView.setData(mWaveData);
        mWavePlotView.redraw();
    }

    void refreshState() {

        log("refreshState!");

        Button buttonTest = (Button) findViewById(R.id.buttonTest);

        //get current audio level
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        mBarMasterLevel.setProgress(currentVolume);

        mTextViewCurrentLevel.setText(String.format("Level: %d/%d", currentVolume,
                mBarMasterLevel.getMax()));

        log("refreshState 2b");

        //get info
        int samplingRate = getApp().getSamplingRate();
        int playbackBuffer = getApp().getPlayBufferSizeInBytes()/getApp().BYTES_PER_FRAME;
        int recordBuffer = getApp().getRecordBufferSizeInBytes()/getApp().BYTES_PER_FRAME;
        StringBuilder s = new StringBuilder(200);
        s.append("SR: " + samplingRate + " Hz");
        int audioThreadType = getApp().getAudioThreadType();
        switch(audioThreadType) {
            case LoopbackApplication.AUDIO_THREAD_TYPE_JAVA:
                s.append(" Play Frames: " + playbackBuffer);
                s.append(" Record Frames: " + recordBuffer);
                s.append(" Audio: JAVA");
            break;
            case LoopbackApplication.AUDIO_THREAD_TYPE_NATIVE:
                s.append(" Frames: " + playbackBuffer);
                s.append(" Audio: NATIVE");
                break;
        }

        //mic source
        int micSource = getApp().getMicSource();
        String micSourceName = getApp().getMicSourceString(micSource);
        if(micSourceName != null) {
            s.append(String.format(" Mic: %s", micSourceName));
        }
/*
        s.append(" App");
*/

        String info = getApp().getSystemInfo();
        s.append(" " + info);

        mTextInfo.setText(s.toString());

/*
        if(mCorrelation.mEstimatedLatencyMs>0.0001) {
            mTextViewEstimatedLatency.setText(String.format("Latency: %.2f ms", mCorrelation.mEstimatedLatencyMs));
        } else {
            mTextViewEstimatedLatency.setText(String.format("Latency: ----"));
        }
*/

    }

    private static void log(String msg) {
        Log.v("Recorder", msg);
    }

    public void showToast(String msg) {

        if(toast == null) {
            toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);

        }



        {
//            toast.setText(msg);
            toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 10, 10);
            toast.show();
        }
    }

    private LoopbackApplication getApp() {
        return (LoopbackApplication) this.getApplication();
    }

    void saveToWavefile(Uri uri) {

       // double [] data = audioThread.getWaveData();
        if (mWaveData != null && mWaveData.length > 0 ) {
            AudioFileOutput audioFileOutput = new AudioFileOutput(getApplicationContext(), uri,
                    mSamplingRate);
            boolean status = audioFileOutput.writeData(mWaveData);

            if (status) {
                showToast("Finished exporting wave File " + mFilePathWav);
//                Toast.makeText(getApplicationContext(), "Finished exporting wave File",
//                        Toast.LENGTH_SHORT).show();
            } else {
                showToast("Something failed saving wave file");
//                Toast.makeText(getApplicationContext(), "Something failed saving wave file",
//                        Toast.LENGTH_SHORT).show();
            }
        }

    }

    void saveScreenShot(Uri uri) {

        boolean status = false;
        ParcelFileDescriptor parcelFileDescriptor = null;
        FileOutputStream outputStream = null;
        try {
            parcelFileDescriptor = getApplicationContext().getContentResolver().openFileDescriptor(uri, "w");

            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            outputStream= new FileOutputStream(fileDescriptor);

            log("Done creating output stream");

            LinearLayout LL = (LinearLayout) findViewById(R.id.linearLayoutMain);

            View v = LL.getRootView();
            v.setDrawingCacheEnabled(true);
            Bitmap b = v.getDrawingCache();
            //BitmapDrawable bitmapDrawable = new BitmapDrawable(b);

            //save
            b.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
//            int sampleCount = data.length;
//            writeHeader(sampleCount);
//            writeDataBufer(data);
//            mOutputStream.close();
            status = true;
            parcelFileDescriptor.close();
            v.setDrawingCacheEnabled(false);
        } catch (Exception e) {
            outputStream = null;
            log("Failed to open png" +e);
        } finally {
            try {
                if (parcelFileDescriptor != null) {
                    parcelFileDescriptor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                log("Error closing ParcelFile Descriptor");
            }
        }

    }

}