snipt

Ctrl+h for KB shortcuts

Java

Save screenshot of your own application

LinearLayout v = (LinearLayout)findViewById(R.id.YourLayoutThatContainsEverything);
if(v != null){
	v.setDrawingCacheEnabled(true);	
	Bitmap bmp = v.getDrawingCache();
	try {
		FileOutputStream out = new FileOutputStream(Environment
				.getExternalStorageDirectory()+"Test.jpg");
		bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
else
	Toast.makeText(this, "View not available in this activity", Toast.LENGTH_LONG);
https://snipt.net/embed/9f2add6ff4b0814435430db29206f6bf/
/raw/9f2add6ff4b0814435430db29206f6bf/
9f2add6ff4b0814435430db29206f6bf
java
Java
16
2019-05-25T08:51:27
True
False
False
/api/public/snipt/19047/
save-bitmap-of-a-view-component
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">LinearLayout</span> <span class="n">v</span> <span class="o">=</span> <span class="o">(</span><span class="n">LinearLayout</span><span class="o">)</span><span class="n">findViewById</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">id</span><span class="o">.</span><span class="na">YourLayoutThatContainsEverything</span><span class="o">);</span> </span><span id="L-2"><a name="L-2"></a><span class="k">if</span><span class="o">(</span><span class="n">v</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">){</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">v</span><span class="o">.</span><span class="na">setDrawingCacheEnabled</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">Bitmap</span> <span class="n">bmp</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="na">getDrawingCache</span><span class="o">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">try</span> <span class="o">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">FileOutputStream</span> <span class="n">out</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FileOutputStream</span><span class="o">(</span><span class="n">Environment</span> </span><span id="L-7"><a name="L-7"></a> <span class="o">.</span><span class="na">getExternalStorageDirectory</span><span class="o">()+</span><span class="s">&quot;Test.jpg&quot;</span><span class="o">);</span> </span><span id="L-8"><a name="L-8"></a> <span class="n">bmp</span><span class="o">.</span><span class="na">compress</span><span class="o">(</span><span class="n">Bitmap</span><span class="o">.</span><span class="na">CompressFormat</span><span class="o">.</span><span class="na">JPEG</span><span class="o">,</span> <span class="mi">90</span><span class="o">,</span> <span class="n">out</span><span class="o">);</span> </span><span id="L-9"><a name="L-9"></a> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">FileNotFoundException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-10"><a name="L-10"></a> <span class="c1">// TODO Auto-generated catch block</span> </span><span id="L-11"><a name="L-11"></a> <span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span> </span><span id="L-12"><a name="L-12"></a> <span class="o">}</span> </span><span id="L-13"><a name="L-13"></a><span class="o">}</span> </span><span id="L-14"><a name="L-14"></a><span class="k">else</span> </span><span id="L-15"><a name="L-15"></a> <span class="n">Toast</span><span class="o">.</span><span class="na">makeText</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> <span class="s">&quot;View not available in this activity&quot;</span><span class="o">,</span> <span class="n">Toast</span><span class="o">.</span><span class="na">LENGTH_LONG</span><span class="o">);</span> </span></pre></div> </td></tr></table>
android