截屏,然后存储在外部存储器中,然后共享

时间:2018-10-28 09:51:32

标签: android android-studio

我想在用户单击“共享”按钮后共享屏幕截图。 我正在尝试应用以下解决方案:https://stackoverflow.com/a/30212385/9748825

这是三种方法:

ManyToManyField

gameDate方法:

class EventAdmin(admin.ModelAdmin):

    def get_field_queryset(self, db, db_field, request):
        if db_field.name == 'event_dates':
            return db_field.remote_field.model.base_manager.all()
        else:
            super(EventAdmin, self).get_field_queryset(db, db_field, request)

触发点:

public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}

public void store(Bitmap bm, String fileName) {
    final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
    File dir = new File(dirPath);
    if (!dir.exists())
        dir.mkdirs();
    File file = new File(dirPath, fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Toast.makeText(this, file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
    shareImage(file);
}

public void shareImage(File file) {
    Uri uri = FileProvider.getUriForFile(iv_ScoreBoard.this, iv_ScoreBoard.this.getApplicationContext().getPackageName() + ".my.package.name.provider", file);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    try {
        startActivity(Intent.createChooser(intent, "Share Screenshot"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(iv_ScoreBoard.this, "No App Available", Toast.LENGTH_SHORT).show();
    }
}

错误:

 public void generateNewGameDate() {
    Date thisSecond = Calendar.getInstance().getTime();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd , HH:mm");
    gameDate = df.format(thisSecond);

}

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我被外部存储权限所困。

应用此答案后,我现在明白了。非常感谢stackoverflow! https://stackoverflow.com/a/37672627/9748825