2016-02-16 24 views
5

我見過以下Link,它並採取截圖與頂端回答如何以編程方式採取截圖處於警戒機器人對話

不過,我要的是爲應用程序採取的截圖警報對話框顯示給用戶,上面的解決方案和下面的代碼只需要對警報對話框背後的內容進行截圖,因此沒有什麼好處

下面是在任何人沒有經過的情況下使用的代碼提供的鏈接

Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 

    try { 
     // image naming and path to include sd card appending name you choose for file 
     String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 

     // create bitmap screen capture 
     View v1 = getWindow().getDecorView().getRootView(); 
     v1.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 

     File imageFile = new File(mPath); 

     FileOutputStream outputStream = new FileOutputStream(imageFile); 
     int quality = 100; 
     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 

     openScreenshot(imageFile); 
    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 

編輯:作爲請求代碼對話框

public void showCalc(String title, String message) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setCancelable(true); 
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.setPositiveButton("Capture + Open", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        //Remove Values From Inventory 
        captureScreenAndOpen(); 

       } 
      }); 

    builder.setNegativeButton("Capture", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        captureScreen(); 
        Context context = getApplicationContext(); 
        Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show(); 
       } 
      }); 

    builder.setNeutralButton("Return", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    builder.show(); 
} 

進一步編輯:

在這裏你會看到兩個截圖,第一個顯示了當一切都從對話框保存的截圖保存的截圖,你」我會注意到底部有一些文字總是出現在底部。

Screenshot1

第二截圖是哪裏有對話的對話是滾動的,這樣你可以看到所有的數據在這麼多的文字,你會發現,在第一張截圖底部串不存在

Screenshot2

如果可能的話,我想它,所有的數據都顯示,我不知道但如果一個截圖功能將能夠做到這一點還是一種替代方法

+0

請發佈您的對話框代碼。 – Rohit5k2

+0

你只想要對話框的截圖嗎?因爲我剛剛開發了一個代碼。 – Rohit5k2

+0

這很有趣:開發一個代碼來回答一個問題。 :D – Rohit5k2

回答

5

開發在Android模擬器5和工作。從您提供的鏈接中獲取對話代碼和屏幕截圖代碼。

這是您的AlertDialog

public void showCalc(String title, String message) { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setCancelable(true); 
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.setPositiveButton("Capture + Open", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        //Remove Values From Inventory 
        captureScreenAndOpen(); 
       } 
      }); 

    builder.setNegativeButton("Capture", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        AlertDialog dialog2 =AlertDialog.class.cast(dialog); 
        takeScreenshot(dialog2); 
        Context context = getApplicationContext(); 
        Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show(); 
       } 
      }); 

    builder.setNeutralButton("Return", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    builder.show(); 
} 

這是截圖代碼

private void takeScreenshot(AlertDialog dialog) { 
    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 

    try { 
     // image naming and path to include sd card appending name you choose for file 
     String mPath = "/data/data/com.rohit.test/test.jpg"; // use your desired path 

     // create bitmap screen capture 
     View v1 = dialog.getWindow().getDecorView().getRootView(); 

     v1.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 

     File imageFile = new File(mPath); 

     FileOutputStream outputStream = new FileOutputStream(imageFile); 
     int quality = 100; 
     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 

    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 
} 

截圖採取

enter image description here

注1:可以概括的實現方法具d takeScreenshot如果將參數類型更改爲View並將dialog.getWindow().getDecorView().getRootView();移動到調用此方法的對話框代碼。

注2:看到您更新的問題。我不認爲當他們中的一些被隱藏時,你可以在屏幕截圖中獲得全部數據。認爲它像一個正常的截圖(在電腦上甚至電話)。你只拍攝你能看到的照片。

+0

謝謝,這段代碼現在工作得很好,差不多... 有時候,消息框可以滾動以適應所有內容,這可能是一個很大的步驟,但是你知道屏幕截圖可以保存所有內容在對話中,而不僅僅是在看什麼? – Sjharrison

+0

我在此代碼中使用的視圖是完整對話框。因此,對話框中的任何內容都將被捕獲。 – Rohit5k2

+0

我會更新我的問題,現在試試更好地解釋 – Sjharrison

0

1.我親愛的朋友您正在做一件錯誤的事,您無法截取對話框的截圖。

View v1 = getWindow().getDecorView().getRootView(); 

您捕捉整個屏幕,這是你的AlertDialog之下

您可以使用這些方法通過發送您的對話框的視圖這種方法

讓你的事情做好

獲取位圖從視圖

public static Bitmap loadView(View v) { 
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    v.layout(0, 0, v.getWidth(), v.getHeight()); 
    v.draw(c); 
    return b; 
} 

保存您的位圖

void saveFile(Bitmap bitmap) { 
    String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder"; 
    String fileName = new SimpleDateFormat("yyyyMMddhhmm'_bitmap.jpg'", Locale.US).format(new Date()); 
    File myPath = new File(extr, fileName); 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(myPath); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

我已經更改了我的代碼以添加您的建議,但是在我的對話框中,我調用了方法,但是它們要求周長,不知道這些會是什麼? – Sjharrison

+0

@Sjharrison你說什麼樣的參數可以提供你的示例代碼 –

+0

如果你看我的代碼上面的對話框我基本上已經創建了一個新的對話框'sendDialogToView();'這是你'從視圖獲取位圖'的代碼和'captureScreenAndOpen();'是你的保存你的位圖代碼 – Sjharrison

相關問題