Thursday 23 April 2015

Android Simple Share Button -5/간단한 쉐어 버튼 만들기...

Introduce how to add share button in your application.

Now I would like to use previous app which I post below.
Use Save button add additional share button under current alert dialog.


1.WebviewActivity.java


...
public void onClick(View v) {
...

...
builder.setItems(new CharSequence[] { "URL Save", "Screenshot",

"Screenshot + Share", "URL Share", "exit" },
...
...
case 2:
// save screenshot
View vi = webview.getRootView();
vi.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(vi
.getDrawingCache());
vi.setDrawingCacheEnabled(true);
String savename = "KarlScreenShot"
+ System.currentTimeMillis() + ".png";
try {
FileOutputStream fos = new FileOutputStream(
new File(
Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
.toString(), savename));
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String imagePath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/" + savename;
Log.d("check imagePath", imagePath);
File imageFileToShare = new File(imagePath);

MediaScannerConnection.scanFile(ctx,
new String[] { imageFileToShare
.getAbsolutePath() }, null,
new OnScanCompletedListener() {
public void onScanCompleted(String path,
Uri uri) {
Log.v("grokkingandroid", "file " + path
+ " was scanned seccessfully: "
+ uri);
}
});
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share,
"Share Image!"));
break;
case 3:
Intent textshare = new Intent(
android.content.Intent.ACTION_SEND);
textshare.setType("text/plain");
textshare
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
textshare.putExtra(Intent.EXTRA_SUBJECT,
currentUrlTitle);
textshare.putExtra(Intent.EXTRA_TEXT, currentUrl);
Log.d("What do you Think", currentUrlTitle + currentUrl);
startActivity(Intent.createChooser(textshare,
"Share link!"));

break;
...

No comments:

Post a Comment