保存到图库中时,如何通过用户为单击的图像指定自定义名称?

时间:2019-04-04 06:28:04

标签: java android

File destination = new File(Environment.getExternalStorageDirectory(),
           System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;

        try {
           // destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

现在,您尝试使用System.currentTimeMillis()随机命名。

但是,如果您希望该用户决定名称,则必须将图像名称作为用户输入。

public void saveImage(String imageName)
File destination = new File(Environment.getExternalStorageDirectory(),
           imageName + ".jpg");

        FileOutputStream fo;

        try {
           // destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 }