2014-04-17 2 views
0

나는 갤러리에서 볼 수 있도록 활동의 스크린 샷을 찍으려고합니다. 그러나 코드가 작동하지 않으며 갤러리에 이미지가 표시되지 않습니다. button click 이벤트에서 takePicSave() 메서드를 호출하고 있습니다. 또한 매니페스트 파일에 사용 권한이 있습니다. 활동의 스크린 샷 캡처 및 sdcard에 저장

private void takePicSave() 
    { 

     Bitmap bitmap = takeScreenshot(); 
     saveBitmap(bitmap); 

    } 

     public Bitmap takeScreenshot(){ 

     View rootview = findViewById(android.R.id.content).getRootView(); 
     rootview.setDrawingCacheEnabled(true); 
     return rootview.getDrawingCache(); 

     } 

     public void saveBitmap(Bitmap bitmap){ 

     Date date = new Date() ; 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); 


      File imagePath = new File(Environment.getExternalStorageDirectory() , dateFormat.format(date) + " .png"); 
      FileOutputStream fos; 
      try { 
       fos = new FileOutputStream(imagePath); 
       bitmap.compress(CompressFormat.JPEG, 100, fos); 
       fos.flush(); 
       fos.close(); 
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 


      } catch (FileNotFoundException e) { 
       Log.e("Error " , e.getMessage(), e); 
      } catch (IOException e) { 
       Log.e("Error ", e.getMessage(), e); 
      } 


     } 



     } 

내가 "java.io.FileNotFoundException/MNT/SDCARD/2014-04-16.png (권한이 거부)

+0

귀하의 목록에 읽기 및 쓰기 권한이 있습니까? – JRowan

+0

게시 오류'LogCat'. –

+0

현재 매니페스트에만 쓰기 권한이 있습니다 – artist

답변

0

내 프로젝트 중 하나는 아래와 같은 기능을 시도 할 수 있습니다,이 작업을 수행 한

public static Bitmap takeScreenShotOfCurrentSticky(Activity activity,View v) 
    { 
     View view = v; 
     view.setDrawingCacheEnabled(true); 
     view.buildDrawingCache(); 
     Bitmap b1 = view.getDrawingCache(); 
     Rect frame = new Rect(); 
     activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
     int statusBarHeight = frame.top; 
     int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 
     int height = activity.getWindowManager().getDefaultDisplay().getHeight(); 

     b = Bitmap.createBitmap(b1); 
     view.destroyDrawingCache(); 
     System.out.println("~~~~Bitmap frm Screen shot"+b); 
     return b; 
    } 

나는이 함수를 Application 클래스에 넣어서 스크린 샷을 찍고 싶은 액티비티 객체와 뷰를 전달했다. 요구 사항에 따라 변경할 수 있습니다.

0

당신이 읽을 수있는 권한을 추가했는지 확인 무엇입니까 오류 및 매니페스트 파일에 외부 저장 장치를 쓰기.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

지금은 읽을 수있는 권한을 추가했지만 여전히 갤러리에 이미지가 표시되지 않습니다 – artist

+0

logcat 오류 스택 추적을 게시하십시오. –

+0

04-17 03 : 43 : 05.292 : E/오류 (377) :/mnt/sdcard/2014-04-17 .png (사용 권한이 거부 됨) 04-17 03 : 43 : 05.292 : E/오류 (377) : java.io.FileNotFoundException :/mnt/sdcard/2014-04-17 .png (허가가 거부되었습니다) 04-17 03 : 43 : 05.292 : E/Error (377) : \t at org.apache.harmony.luni.platform .OSFileSystem.open (네이티브 메서드) 04-17 03 : 43 : 05.292 : E/오류 (377) : \t at dalvik.system.BlockGuard $ WrappedFileSystem.open (BlockGuard.java:232) 04-17 03:43 : 05.292 : E/Error (377) : \t at java.io.FileOutputStream. (FileOutputStream.java:94) 04-17 03 : 43 : 05.292 : E/오류 (377) : – artist