我正在制作一个应用程序,它需要专门截取屏幕截图,并且应该能够将其作为图像文件共享。我还需要在Watsapp和Facebook的图像中添加一个超链接作为标题。 我使用下面的代码共享视图的图像。需要更多关于如何包含文件链接的建议。
XML File for Button to Create a image and Assign to ImageView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_text"
android:id="@+id/send_text"
android:layout_below="@+id/brands"
android:onClick="sendMessage"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
班级代码
public void sendMessage(View view)
{
//Get a reference to EditText
EditText editText = (EditText)findViewById(R.id.message);
String msg = editText.getText().toString();
System.out.println(msg);
Intent intent = new Intent(this,BeerMessageActivity.class);
intent.putExtra(BeerMessageActivity.EXTRA_MESSAGE, msg);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
Bitmap b = snap(view.getRootView());
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
intent.putExtra(BeerMessageActivity.EXTRA_IMAGE, bs.toByteArray());
/*intent.putExtra(BeerMessageActivity.EXTRA_IMAGE,snap(view));*/
startActivity(intent);
}
public static Bitmap snap(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
ImageView上的Onclick事件
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/message"
android:id="@+id/img_test"
android:onClick="shareit"/>
共享功能代码
public void shareit(View view)
{
ImageView imageView = (ImageView)findViewById(R.id.img_test);//your layout id
Bitmap app_snap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SaveImg";
System.out.println("****FILEPATH **** : " + file_path);
File imagePath = new File(Environment.getExternalStorageDirectory() + "/scr.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
System.out.println("****FILEPATH1 **** : " + file_path);
app_snap.compress(Bitmap.CompressFormat.PNG, 100, fos);
System.out.println("****FILEPATH2 **** : " + file_path);
fos.flush();
fos.close();
}
catch (IOException e) {
System.out.println("GREC****** "+ e.getMessage());
}
Intent sharingIntent = new Intent();
Uri imageUri = Uri.parse(imagePath.getAbsolutePath());
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);
}
请告知是否需要转换为字节数组,否则我们可以执行此操作。 我到了上面代码共享图片的地方。通过watsapp / facebook / Google +。