延迟在画廊保存图片

时间:2017-03-26 18:41:57

标签: java android

Wassup伙计们,我正在做一个大学项目,我的应用程序是一个模因生成器。在文本绘制后,我正在尝试按下按钮保存图像。吐司出现了,但是,画廊上的图片显示需要一个多小时。我怎样才能立即保存这个东西?

所以有我的代码来创建按钮监听器和商店功能

    save.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            View content  = findViewById(R.id.lay);
            Bitmap bitmap = getScreenShot(content);
            currentImage = "meme" + System.currentTimeMillis() + ".png";
            store(bitmap, currentImage);
            share.setEnabled(true);
        }

    });




    public void store(Bitmap bm, String fileName){
    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Memes";
    File dir  = new File(dirPath);
    if(!dir.exists()){
        dir.mkdir();
    }
    File file = new File(dirPath, fileName);
    try{
        FileOutputStream fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        Toast.makeText(this, "SALVOU ARROMBADO", Toast.LENGTH_SHORT).show();
    }catch (Exception e){
        Toast.makeText(this, "DEU RUIM VACILÃO", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

您必须使用下面的MediaScannerConnection。创建方法refreshMedia() -

public void refreshMedia(){

 new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
    MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
              new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                //gallery refreshed.
                  Log.i(TAG, "Scanned " + path);
                }
              });
           }
        }, 1500);  //this is to refresh media after 1.5 second delay

}

此处imageFile.getPath()是您图片的路径。

使用如下 -

save.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){

             store(bitmap, currentImage);
            //after saving image call refreshmedia()
             refreshMedia();
        }