Created
June 23, 2017 17:47
-
-
Save tank777/441d79e09298735c3f01f4871ceb55a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bgt.viewtopdf; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.media.MediaScannerConnection; | |
import android.net.Uri; | |
import android.os.Environment; | |
import android.support.v4.widget.NestedScrollView; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import com.tarek360.instacapture.Instacapture; | |
import com.tarek360.instacapture.listener.SimpleScreenCapturingListener; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MAIN" ; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void mPDF(View view) { | |
// Instacapture.INSTANCE.capture(this, new SimpleScreenCapturingListener() { | |
// @Override | |
// public void onCaptureComplete(Bitmap bitmap) { | |
// if (bitmap!=null){ | |
// Log.e(TAG, "onCaptureComplete: "+String.valueOf(bitmap) ); | |
// File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Image"); | |
// | |
// if (!mediaStorageDir.exists()) { | |
// if (!mediaStorageDir.mkdirs()) { | |
// Log.d("App", "failed to create directory"); | |
// } | |
// } | |
// try { | |
// File file = new File(mediaStorageDir, "share_image_" + System.currentTimeMillis() + ".png"); | |
// FileOutputStream out = new FileOutputStream(file); | |
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
// | |
// MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.toString()}, null, | |
// new MediaScannerConnection.OnScanCompletedListener() { | |
// public void onScanCompleted(String path, Uri uri) { | |
// | |
// } | |
// }); | |
// } | |
// catch (Exception e){ | |
// e.fillInStackTrace(); | |
// } | |
// | |
// | |
// | |
// } | |
// | |
// } | |
// }, (View) findViewById(R.id.bt)); | |
NestedScrollView z = (NestedScrollView) findViewById(R.id.scroll_view); | |
int totalHeight = z.getChildAt(0).getHeight(); | |
int totalWidth = z.getChildAt(0).getWidth(); | |
Bitmap screenShot = loadBitmapFromView(findViewById(R.id.scroll_view),totalWidth,totalHeight); | |
//Bitmap screenShot = mGetBitmap(findViewById(R.id.scroll_view)); | |
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Image"); | |
if (!mediaStorageDir.exists()) { | |
if (!mediaStorageDir.mkdirs()) { | |
Log.d("App", "failed to create directory"); | |
} | |
} | |
try { | |
File file = new File(mediaStorageDir, "share_image_" + System.currentTimeMillis() + ".png"); | |
FileOutputStream out = new FileOutputStream(file); | |
screenShot.compress(Bitmap.CompressFormat.JPEG, 100, out); | |
out.close(); | |
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null, | |
new MediaScannerConnection.OnScanCompletedListener() { | |
public void onScanCompleted(String path, Uri uri) { | |
} | |
}); | |
} | |
catch (Exception e){ | |
e.fillInStackTrace(); | |
} | |
} | |
private Bitmap mGetBitmap(View view) { | |
view.setDrawingCacheEnabled(true); | |
NestedScrollView z = (NestedScrollView) findViewById(R.id.scroll_view); | |
int totalHeight = z.getChildAt(0).getHeight(); | |
int totalWidth = z.getChildAt(0).getWidth(); | |
view.layout(0, 0, totalWidth, totalHeight); | |
view.buildDrawingCache(true); | |
Bitmap b = Bitmap.createBitmap(view.getDrawingCache()); | |
view.setDrawingCacheEnabled(false); | |
return b; | |
} | |
public static Bitmap loadBitmapFromView(View v, int width, int height) { | |
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888); | |
Canvas c = new Canvas(b); | |
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); | |
v.draw(c); | |
return b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment