title: Android save bitmap to image file
body: <h3>How to save bitmap to image file? (png/jpg any types don't care...)</h3>
  <p>I'm running device(hisilicon) &amp; android app.<br />
  Android and device are communicating over sockets.<br />
  Device send Image (h.264) and show it on Android on TextureView.<br />
  I can get Bitmap from TextureView, using <code>textureView.getBitmap()</code>.</p>
  <hr />
  <p>I made a button button to save the picture, <code>textureView.getBitmap()</code>.</p>
  <pre class="lang-js prettyprint-override"><code>&lt;Button
    android:id=&quot;@+id/btnSavePng&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:onClick=&quot;onBtnSavePng&quot;
    android:text=&quot;savePicture&quot; /&gt;
  </code></pre>
  <p>and, onclick function is like under.</p>
  <pre class="lang-java prettyprint-override"><code>
  public String getCurrentTimeString() {
    int yyyy = Calendar.getInstance().get(Calendar.YEAR);
    int MM = Calendar.getInstance().get(Calendar.MONTH) + 1;
    int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int hh = Calendar.getInstance().get(Calendar.HOUR);
    int mm = Calendar.getInstance().get(Calendar.MINUTE);
    int ss = Calendar.getInstance().get(Calendar.SECOND);
    
    String result = yyyy+&quot;-&quot;+MM+&quot;-&quot;+dd+&quot; &quot;+hh+&quot;:&quot;+mm+&quot;:&quot;+ss;
    return result;
  }
  
  public void onBtnSavePng(View view) {
    try {
      File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
      String fname = getCurrentTimeString() + &quot;.jpg&quot;;
      File tp = new File(storage, fname);
      Bitmap bm = textureView.getBitmap();
      tp.createNewFile(); // Result of File.createNewFile() ignored
  
      FileOutputStream ot = new FileOutputStream(tp);
      bm.compress(Bitmap.CompressFormat.JPEG, 100, ot);
      ot.close();
    } catch(Exception e) {
      Log.d(&quot;onBtnSavePng&quot;, e.toString()); // java.io.IOException: Operation not permitted
    }
  }
  </code></pre>
  <p>I allow uses-permissions <code>AndroidManifest.xml</code> like under, and <code>android:requestLegacyExternalStorage=&quot;true&quot;</code> is on application.</p>
  <pre class="lang-js prettyprint-override"><code>    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.CHANGE_WIFI_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.CHANGE_WIFI_MULTICAST_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.CHANGE_NETWORK_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.GET_TASKS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.RECORD_AUDIO&quot; /&gt;
  </code></pre>
  <p>Is there any problem with my codes?</p>
  <p>If so, how to save bitmap to png or jpg file?</p>
  <p>I guess, my app don't access to directory.</p>
  <p>Thank you for read my question.</p>
  <hr />
  <h2>Self Solved.</h2>
  <pre class="lang-java prettyprint-override"><code>
  public void onBtnSavePng(View view) {
    try {
      String root = Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_PICTURES).toString();
      File myDir = new File(root + &quot;/saved_images&quot;);
      myDir.mkdirs();
      String fname = getCurrentTimeString() + &quot;.jpg&quot;;
      File file = new File(myDir, fname);
  
      FileOutputStream out = new FileOutputStream(file);
      Bitmap bm = textureView.getBitmap();
      bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
      out.flush();
      out.close();
    } catch( Exception e) {
      Log.d(&quot;onBtnSavePng&quot;, e.toString());
    }
  
  </code></pre>
  <p>I don't understand why my code is worked not yet.<br />
  Anyway, Thank you for all who helps me.<br />
  I'll try all of reply codes.</p>
  <p>Thank you.</p>