之前写过一篇如何在安卓APP中添加分享按钮?,这个也不是说没用,但是再上架Google Play Store 时被拒了(如下图所示),大意是赋予的读写权限过大以及不符合规范。
布局及相关权限
- AndroidManifest.xml权限添加
<!-- STORAGE PERMISSION FOR SCREEN SHOT-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 布局
...
<LinearLayout android:id="@+id/shared_preferences_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7E03A9F4">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_weight="10"
android:text="@string/share_info"/>
<Button android:id="@+id/share"
android:background="@drawable/ic_share"
android:layout_weight="1"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
...
Code
- 主要引用
...
Button share = (Button) findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot(gameWeb);
try {
saveImage(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
shareIt(GameActivity.this);
}
});
...
- 截图
public Bitmap takeScreenshot(View v) {
View rootView = findViewById(android.R.id.content);
Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(),rootView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
rootView.draw(canvas);
return bitmap;
}
- 存图
private void saveImage(Bitmap bitmap) throws IOException {
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmss");
String name = "HighTower_"+ft.format(dNow);
OutputStream fos;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File image = new File(imagesDir, name);
fos = new FileOutputStream(image);
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Objects.requireNonNull(fos).close();
}
- 分享
private void shareIt (Context context){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = getString(R.string.shareBody);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.Score));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM,imageUri);
startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));
}