Reputation: 214
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 !
Upvotes: 0
Views: 428
Reputation: 6406
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;
}
Upvotes: 1
Reputation: 28823
Add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your manifest file.
Hope this helps.
Upvotes: 1