2015-05-04 3 views
12

कोड पहले स्क्रीनशॉट के लिए ठीक काम करता है और किसी अन्य दृश्य में जाने के बावजूद एक ही स्क्रीनशॉट लेता रहता है।एंड्रॉइड - वर्तमान स्क्रीन का स्क्रीनशॉट नहीं ले रहा

कैसे वर्तमान स्क्रीनशॉट पाने के लिए?

public void saveBitmap(Bitmap bitmap) { 

    File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date())); 
    FileOutputStream fos =null; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 

क्लिक जानकारी:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.iSave: 
      Bitmap bitmap = null; 
      bitmap = takeScreenshot(); 
      saveBitmap(bitmap); 
     break; 
    } 
} 
यहाँ

:

public Bitmap takeScreenshot() { 
    View rootView = findViewById(android.R.id.content).getRootView(); 
    rootView.setDrawingCacheEnabled(true); 
    return rootView.getDrawingCache(); 
} 
+1

पोस्ट takeScreenshot() विधि:

private final static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots"; public static void store(Bitmap bm, String fileName){ File dir = new File(dir); if(!dir.exists()) dir.mkdirs(); File file = new File(dir, fileName); try { FileOutputStream fOut = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.PNG, 85, fOut); fOut.flush(); fOut.close(); } catch (Exception e) { e.printStackTrace(); } } 

At last, स्क्रीनशॉट फ़ाइल को साझा करें। –

+0

तैनात .. @rajatmehra – Fou

+0

GetWindow()। GetDecorView() की कोशिश करो। GetRootView() के बजाय findViewById की (android.R.id.content) .getRootView()। –

उत्तर

15

कॉल rootView.setDrawingCacheEnabled(false); स्क्रीन शॉट लेने के बाद। इसे बंद करना और फिर फिर से इसे सही ढंग से अपडेट करने के लिए मजबूर करता है।

public Bitmap takeScreenshot() { 
    View rootView = findViewById(android.R.id.content).getRootView(); 
    rootView.setDrawingCacheEnabled(true); 
    Bitmap bitmap = rootView.getDrawingCache(); 
    rootView.setDrawingCacheEnabled(false); 
    return bitmap; 
} 
+0

धन्यवाद काम .....;) – Fou

2

मैंने कभी भी वर्तमान Activity पर कब्जा करने की कोशिश की है और फिर स्क्रीनशॉट साझा करें। नीचे यह है कि मैंने कैसे किया, यदि आप अभी भी रुचि रखते हैं तो उन्हें देखें, और मुझे लगता है कि आप सहमत होंगे।

पहले, वर्तमान Activity की जड़ दृश्य प्राप्त:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content); 

या

View rootView = findViewById(android.R.id.content); 

या

View rootView = findViewById(android.R.id.content).getRootView(); 

दूसरा, वें से Bitmap मिल ई जड़ दृश्य:

public static Bitmap getScreenShot(View view) { 
    View screenView = view.getRootView(); 
    screenView.setDrawingCacheEnabled(true); 
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); 
    screenView.setDrawingCacheEnabled(false); 
    return bitmap; 
} 

तीसरा, SDCard में Bitmap की दुकान:

private void shareImage(String file){ 
    Uri uri = Uri.fromFile(file); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.setType("image/*"); 
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ""); 
    intent.putExtra(android.content.Intent.EXTRA_TEXT, ""); 
    intent.putExtra(Intent.EXTRA_STREAM, uri); 
    startActivity(Intent.createChooser(intent, "Share Screenshot")); 
} 
संबंधित मुद्दे