2013-07-19 192 views
2

當前,我陷入了一個問題,那就是如何截取Google地圖v2的截圖。我正在開發一個GPS跟蹤應用程序,其中一條多段線正在繪製跟蹤路徑。所以我想要在地圖上繪製多段線的地圖的截圖。Android谷歌地圖V2截圖

我做了很多R & D但發現Google map V2沒有提供這樣的設備。

請幫我解決這個問題。

+0

你能請告訴我們怎樣才能管理整個跟蹤路線將在截圖中,因爲我們可以截圖的截圖,但不知怎的,它不讓我們有一次完整的路徑旅行。 – Harpreet

回答

1

由於您使用的是v2,因此您的地圖將被管理爲MapFragment或SupportMapFragment。兩者都是Fragment的子類,它應該可以訪問getView()。讓我們假設你有下面的代碼在您的主機的活動:

SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map)); 
View view = mapFragment.getView(); 

這個觀點你可以試試這個方法How to programmatically take a screenshot in Android?

+0

感謝您的回答,我認爲這將起作用。 –

+0

我已經試過這段代碼,但遺憾的是,你知道它不工作。 –

+0

它崩潰了嗎?你無法保存圖片?該圖像沒有顯示截圖?請說明你的問題。 – a11n

1

我根據這個帖子: Capture screen shot of GoogleMap Android API V2

和管理,以獲得與覆蓋地圖的截圖,沒有任何問題:

下面是代碼:

public void captureScreen() 
    { 
     GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() 
     { 


      @Override 
      public void onSnapshotReady(Bitmap snapshot) { 
       try { 
        getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true); 
        Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache(); 
        Bitmap bmOverlay = Bitmap.createBitmap(
          backBitmap.getWidth(), backBitmap.getHeight(), 
          backBitmap.getConfig()); 
        Canvas canvas = new Canvas(bmOverlay); 
        canvas.drawBitmap(snapshot, new Matrix(), null); 
        canvas.drawBitmap(backBitmap, 0, 0, null); 

        OutputStream fout = null; 

        String filePath = System.currentTimeMillis() + ".jpeg"; 

        try 
        { 
         fout = openFileOutput(filePath, 
           MODE_WORLD_READABLE); 

         // Write the string to the file 
         bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fout); 
         fout.flush(); 
         fout.close(); 
        } 
        catch (FileNotFoundException e) 
        { 
         // TODO Auto-generated catch block 
         Log.d("ImageCapture", "FileNotFoundException"); 
         Log.d("ImageCapture", e.getMessage()); 
         filePath = ""; 
        } 
        catch (IOException e) 
        { 
         // TODO Auto-generated catch block 
         Log.d("ImageCapture", "IOException"); 
         Log.d("ImageCapture", e.getMessage()); 
         filePath = ""; 
        } 

        openShareImageDialog(filePath); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 

     ; 


     map.snapshot(callback); 
    } 

然後我用這個方法用於社交媒體分享:

 public void openShareImageDialog(String filePath) 
     { 
      File file = this.getFileStreamPath(filePath); 

      if(!filePath.equals("")) 
      { 
       final ContentValues values = new ContentValues(2); 
       values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
       values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); 
       final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

       final Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
       intent.setType("image/jpeg"); 
       intent.putExtra(android.content.Intent.EXTRA_STREAM, contentUriFile); 
       startActivity(Intent.createChooser(intent, "Share Image")); 
      } 
      else 
      { 
       //This is a custom class I use to show dialogs...simply replace this with whatever you want to show an error message, Toast, etc. 
       // DialogUtilities.showOkDialogWithText(this, R.string.shareImageFailed); 

       Toast.makeText(getBaseContext(),"Se pelo",Toast.LENGTH_SHORT).show(); 
      } 
     }