简体   繁体   English

延迟将图片保存在图库中

[英]Delay saving picture on gallery

Wassup Guys, i'm doing a college project, and my app is a meme generator. Wassup伙计们,我正在做一个大学项目,我的应用程序是一个模因生成器。 After the text plotted, i'm trying to save the image pushing the button. 绘制文本后,我正在尝试保存图像并按下按钮。 The toast appears, but, it takes more than an hour to the pic show on the gallery. 吐司虽然出现了,但是画廊上的图片展示却花了一个多小时。 How can i make this thing save immediately? 我该如何立即保存此东西?

So there are my codes to create the button listener and the store function 所以我有一些代码来创建按钮监听器和存储功能

    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();
    }
}

You have to use MediaScannerConnection like below .Create a method refreshMedia() - 你必须使用MediaScannerConnection像下面.Create方法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

}

Here imageFile.getPath() is path for your image. 这里的imageFile.getPath()是图像的路径。

Use it as below- 如下使用它

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM