2015-12-30 6 views
10

मैंने ऐप विकसित किया है जो स्क्रीनशॉट लेता है। लेकिन यह केवल ऐप का स्नैपशॉट लेता है। मैं ऐप से स्नैपशॉट लेना चाहता हूं। मैंने जवाबों का शोध किया है लेकिन मुझे अभी तक जवाब नहीं मिला है। यहां मेरा कोड है।स्क्रीन के स्नैपशॉट को कैसे न लें कोड के साथ एंड्रॉइड में ऐप न केवल

View view = getWindow().getDecorView().getRootView(); 
view.setDrawingCacheEnabled(true); 
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
view.setDrawingCacheEnabled(false); 
saveImageToAppFolder(bitmap); 

saveImagetoAppFolder फ़ंक्शन है जो ऐप फ़ोल्डर में छवि सहेजता है। यह समस्या नहीं है। क्या स्क्रीन के स्नैपशॉट लेने के लिए वैसे भी है?

नोट::

+0

आपको उस – insomniac

+0

के लिए रूट अनुमतियों की आवश्यकता है बेशक, मुझे यह पता है। लेकिन मैं रूट अनुमति कैसे प्राप्त कर सकता हूं? – Dima

+0

केवल तभी यदि आपके डिवाइस को रूट किया गया है, तो क्या आप जानना चाहते हैं कि रूट का उपयोग करके स्क्रीनशॉट कैसे करें? – insomniac

उत्तर

4

डिवाइस स्क्रीन के स्क्रीन शॉट लेने के लिए, सिर्फ अगर आपके पास रूट कॉल की तरह screencap बाइनरी:

Process sh = Runtime.getRuntime().exec("su", null,null); 
OutputStream os = sh.getOutputStream(); 
os.write(("/system/bin/screencap -p " + Environment.getExternalStorageDirectory()+ "/img.png").getBytes("ASCII")); 
os.flush(); 
os.close(); 
sh.waitFor() 

और एक बिटमैप में उस फ़ाइल को लोड, उपयोग

public static Bitmap decodeSampledBitmapFromFile(String path, 
                int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(path, options); 
    } 

    public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 

     return inSampleSize; 
    } 
+0

जहां फाइलें सहेजी गई हैं? अगर मैं इसे ऐप फ़ोल्डर सहेजना चाहता हूं तो मैं इसे कैसे कर सकता हूं? – Dima

+0

फ़ाइल को sdcard – insomniac

+0

में img.png के रूप में सहेजा गया है यदि आप इसे किसी अन्य निर्देशिका में सहेजना चाहते हैं, तो निर्देशिका नाम का उपयोग करें या यदि आप इसे ऐप के डेटा फ़ोल्डर में चाहते हैं तो पर्यावरण के बजाय Environment.getDataDirectory() या getFilesDir() का उपयोग करें। getExternalStorage() – insomniac

0

मैं saveImageToAppFolder में अपने कोड नहीं पता है कि लेकिन आप इस कोशिश कर सकते हैं है आप पारदर्शी (100%) के लिए अपने app/गतिविधि की पृष्ठभूमि सेट की जरूरत है।

// पहले:

//your code below is extractly 
View view = getWindow().getDecorView().getRootView(); 
view.setDrawingCacheEnabled(true); 
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
view.setDrawingCacheEnabled(false); 

    //try my code for save image file to storage 
    File imgFile = new File(imgPath); 

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

कोड पारदर्शी पृष्ठभूमि सेट करने के लिए

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent"> 

</activity> 

टिप्पणी: इस तरह से सेट करने के बाद के लिए पारदर्शी

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:backgroundDimEnabled">false</item> 
    </style> 
</resources> 

नीचे विषय एक्सएमएल बनाने आप यहां यूआरएल से अधिक विस्तार कर सकते हैं: How do I create a transparent Activity on Android?

+0

से नीचे अपना उत्तर देख सकते हैं यह उत्तर केवल ऐप स्क्रीन को स्क्रीनशॉट करता है, पूरे प्रश्न जैसे पूरे प्रश्न – insomniac

+0

ओह।यह मेरा संकल्प है: हर कोई पारदर्शी होने के लिए आपके आवेदन का बैकग्राउंड सेट कर सकता है ताकि छवि पर कब्जा कर लिया गया सब कुछ स्क्रीन पर दिखाया जा रहा हो, न कि आपकी गतिविधि – GiapLee

+0

लेकिन ऐप में कई बटन हैं और कुछ और है। मैं पृष्ठभूमि पारदर्शी कैसे सेट कर सकता हूँ? – Dima

0

यहां आप स्क्रीन को कैप्चर कर सकते हैं और इसे अपने स्टोरेज में कैसे सहेज सकते हैं

बाहरी संग्रहण में फ़ाइल बनाने के लिए अनुमतियां

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

और यह गतिविधि के लिए कोड है।

private void takeScreenshot() { 
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(); 
} 

}

इस तरह आप स्क्रीन पर कब्जा कर सकते हैं।

संबंधित मुद्दे