I'm trying to screenshot a ScrollView and simply copy+pasted the solution from here: Taking a "screenshot" of a specific layout in Android . Unfortunately I'm getting a NP @ Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
My XML is
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollingProfile"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content"
android:background="@drawable/tile_tan_repeat">
<LinearLayout
android:id="@+id/paidLayoutLinearParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--......-->
</LinearLayout>
</ScrollView>
Anyone have any ideas of why this is happening? I've never really worked with saving files like this so debugging this code is pretty unfamiliar to me.
Thank you!
Edit:This is the code I'm using
public void onClick(View v) {
if(v.getId()==R.id.screenshot){//source: https://stackoverflow.com/questions/10650049/taking-a-screenshot-of-a-specific-layout-in-android
View u = findViewById(R.id.scrollingProfilePaid);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.scrollingProfilePaid);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
//String fileName = new SimpleDateFormat("yyyyMMddhhmm'profile.jpg'").format(new Date());
String fileName = "profile.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}