如何将RelativeLayout保存到相册中?

时间:2019-04-11 01:41:51

标签: java android

我目前正在开发一个应用程序,该程序允许我将撞球拖放到撞球台上以绘制镜头。我希望能够将布局保存到相册中,以便用户在练习时可以查看它们。我无法正常工作。

我尝试了各种方法,包括将布局保存为位图,但没有任何效果。

我还在AndroidManifest中加入了这一行: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

当我单击按钮时,它将调用以下saveImage()方法:

public void saveImage() {
    Context context = getApplicationContext();
    String saveSuccess = "Your JotShot has been saved!";
    String saveFail = "There was an error saving your JotShot!";
    int duration = Toast.LENGTH_SHORT;

    // use JotShot followed by current time for file name
    final String name = "JotShot" + System.currentTimeMillis() + ".jpg";

    table.setDrawingCacheEnabled(true);
    jotShotLayout.buildDrawingCache();
    bitmap = jotShotLayout.getDrawingCache();
    // insert the image on the device
    String location = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, name, "JotShot Diagram");

    if (location != null) {
        // display a message indicating that the image was saved
        Toast message = Toast.makeText(context, saveSuccess, duration);
        message.setGravity(Gravity.CENTER, message.getXOffset() / 2,
              message.getYOffset() / 2);
        message.show();
    }
    else {
        // display a message indicating that there was an error saving
        Toast message = Toast.makeText(context, saveFail, duration);
        message.setGravity(Gravity.CENTER, message.getXOffset() / 2,
              message.getYOffset() / 2);
        message.show();
    }
}

我希望弹出权限对话框,并在允许权限后将活动布局保存到相册。但是我却收到错误消息。

0 个答案:

没有答案