首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ListView保存屏幕快照,打开屏幕快照不会打开最新版本

问从ListView保存屏幕快照,打开屏幕快照不会打开最新版本
EN

Stack Overflow用户
提问于 2017-08-22 15:24:43
回答 1查看 34关注 0票数 0

我试图将整个ListView的屏幕截图保存在内存中,并将其发送到其他应用程序。但如果我再做一次,屏幕截图就不是最新的了。这是相同的截图,但我在发送之前更改了数据。

我使用以下代码创建屏幕快照:

代码语言:javascript
复制
  public static Bitmap getWholeListViewItemsToBitmap(SearchableAdapter adapter, ListView listview ) {
        int itemscount       = adapter.getCount();
        int allitemsheight   = 0;
        List<Bitmap> bmps    = new ArrayList<>();

        for (int i = 0; i < itemscount; i++) {

            View childView = adapter.getView(i, null, listview);
            childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            childView.destroyDrawingCache();
            childView.buildDrawingCache();
            bmps.add(childView.getDrawingCache());
            allitemsheight+=childView.getMeasuredHeight();
        }

        Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
        Canvas bigcanvas    = new Canvas(bigbitmap);

        Paint paint = new Paint();
        int iHeight = 0;

        for (int i = 0; i < bmps.size(); i++) {
            Bitmap bmp = bmps.get(i);
            bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
            iHeight+=bmp.getHeight();

            bmp.recycle();
            bmp=null;
        }
        return bigbitmap;
    }

此代码保存屏幕截图:

代码语言:javascript
复制
 public static boolean storeScreenshot(Context context, Bitmap screenshot) {
        try {
            File imagePath = new File(context.getCacheDir(), "images");
            if (!imagePath.exists())
                imagePath.mkdirs();
            File newFile = new File(imagePath, "image.png");
            if (newFile.exists()){
                newFile.delete();
                newFile = new File(imagePath, "image.png");
            }
            FileOutputStream stream = new FileOutputStream(newFile.getAbsolutePath()); // overwrites this image every time
            screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
            stream.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

此代码打开屏幕截图并使用共享意图发送:

代码语言:javascript
复制
try {
    Helper.storeScreenshot(getApplicationContext(), Helper.getWholeListViewItemsToBitmap(adapter, listview));
    File imagePath = new File(getApplicationContext().getCacheDir(), "images");
    File screenshot = new File(imagePath, "image.png");
    Uri contentUri = null;
    if (screenshot.exists())
      contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.myApp.fileprovider", screenshot);

    if (contentUri != null) {
      Intent shareIntent = new Intent();
      shareIntent.setAction(Intent.ACTION_SEND);

      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
      shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);

      startActivity(Intent.createChooser(shareIntent, "App auswählen"));
     }
 }
 catch(Exception e){
 Toast.makeText(getApplicationContext(),"Fehler beim Erstellen des Screenshots!", Toast.LENGTH_LONG);
 }

如果我再次尝试用更改过的数据共享Listview,它总是发送一些旧的屏幕快照。

有什么主意吗?

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-23 15:30:45

解决办法是

“如果文件名保持不变,缓存就不会更新。我通过删除目录的内容并以毫秒为单位将文件名设置为当前日期来解决这个问题。”- Gurupad Mamadapur Apr 12在17:00

因此,我将保存方法的代码更改为:

代码语言:javascript
复制
public static String storeScreenshot(Context context, Bitmap screenshot) {
        try {
            File imagePath = new File(context.getCacheDir(), "images");
            if (!imagePath.exists())
                imagePath.mkdirs();
            File[] imagePathDir = imagePath.listFiles();
            if(imagePathDir != null){
                for (File file : imagePathDir ){
                    file.delete();
                }
            }
            File screenshotFile = new File(imagePath, System.currentTimeMillis()+".png");
            FileOutputStream stream = new FileOutputStream(screenshotFile.getAbsolutePath());
            screenshot.compress(Bitmap.CompressFormat.PNG, 100, stream);
            stream.close();
            return screenshotFile.getName();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

它返回文件名,现在它起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45821634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档