2017-08-22 9 views
0

ListViewのスクリーンショットを内部メモリに保存して他のアプリに送信しようとしています。しかし、私が再びそれを行うと、スクリーンショットは最新ではありません。同じスクリーンショットですが、送信する前にデータを変更しました。ListView、Openからスクリーンショットを保存するスクリーンショットは最新バージョンを開きません

私はスクリーンショットを作成するための次のコードを使用

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

このコードは、スクリーンショットを保存します。

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

このコードは、スクリーンショットを開き、それを送信するためにshareintentを使用します。

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

リストビューを変更したデータと再び共有しようとすると、いくつかの古いスクリーンショット。

誰かアイデア?ファイル名が同じままであれば

どうもありがとう

+0

私は答えがhttps://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to-save-itのコメントで見つかったと思います-prior-sharing ---> "ファイル名が同じであればキャッシュは更新されません。ディレクトリの内容を削除し、ファイル名を現在の日付にミリ秒単位で設定することで解決しました。 - Gurupad Mamadapur Apr 12 at 17: 00 "となる。私はこれを試してみると、私は連絡を取るでしょう。 – Neo1989

答えて

0

ソリューションは

「キャッシュが更新されないのである。私は、ディレクトリの内容を削除し、 にファイル名を設定することで、この を解決しました。

: - ミリ秒単位での現在の日付Gurupad Mamadapur 4月12日17:00"

でだから私はに保存する方法のコードを変更しました

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 ""; 
     } 

これはファイル名を返します。これで動作します。

関連する問題