0

I need in my project to make a snapshot for current activity (whole main layout) in Android, I attempt many ways but always I get error which "open failed eacces (permission denied) at cretenewFile()" My code like this:

public void TakeSnapshoot(){
        mainlayout.setDrawingCacheEnabled(true);
        Bitmap b = mainlayout.getDrawingCache();

        String extr = Environment.getExternalStorageDirectory().toString()
                + "/SaveCapture";
       File myPath = new File(extr);

        if (!myPath.exists()) {
            boolean result = myPath.mkdir();
            Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
        }
        myPath = new File(extr, getString(R.string.app_name) + ".jpg");

        Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
        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) {
             e.printStackTrace();
            //Log.e("Error", e + "");
        }

        catch (Exception e) {
             e.printStackTrace();
            //Log.e("Error", e + "");
        }

}

The AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="SOA.com"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
 <!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- For android different screen sizes -->
    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
    /> ... 

after take a snapshot then I can print what I want!? I thought this way is best way to print current activity in android?! Please any idea or help will be appreciated !

Maheera Jazi
  • 214
  • 1
  • 6
  • 19

2 Answers2

1

Add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in your manifest file.

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

this function will give you a bit map of current view.

Bitmap getBitmapFromView(View v) {
    v.getRootView().measure(
            MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,
                    MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(v.getLayoutParams().height,
                    MeasureSpec.EXACTLY));
    // v.getp
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
            Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
}
nitesh goel
  • 6,338
  • 2
  • 29
  • 38
  • But please, I need to take a snapshot for all data in ListView Can I? by using your function? Thanks anyway ... – Maheera Jazi Dec 10 '13 at 11:41
  • this function will give you a screenshot of the full screen. so whatever is on the screen you will get it capture. – nitesh goel Dec 11 '13 at 04:57
  • I know that! but for the list view it contain scroll view so there are data will not appear in screenshot! ,,, – Maheera Jazi Dec 11 '13 at 07:00
  • Oh! you can not get that because list view populate only those views which are visible on the screen.you can not get a snapshot of a view which is not created. – nitesh goel Dec 11 '13 at 07:16
  • Thanks! I will search for another solution ,, I wondered to use PDF functions to get all List view data! then I can print them! ... – Maheera Jazi Dec 11 '13 at 07:49