2012-04-15 11 views
6

मैं उस एप्लिकेशन को विकसित कर रहा हूं जिसे वॉलपेपर के रूप में एक छवि सेट करने की आवश्यकता है।वॉलपेपर को प्रोग्रामेटिक रूप से वॉलपेपर के रूप में कैसे सेट करें?

कोड:

WallpaperManager m=WallpaperManager.getInstance(this); 

String s=Environment.getExternalStorageDirectory().getAbsolutePath()+"/1.jpg"; 
File f=new File(s); 
Log.e("exist", String.valueOf(f.exists())); 
try { 
     InputStream is=new BufferedInputStream(new FileInputStream(s)); 
     m.setBitmap(BitmapFactory.decodeFile(s)); 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("File", e.getMessage()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("IO", e.getMessage()); 
    } 

इसके अलावा, मैं निम्नलिखित अनुमति जोड़ लिया है:

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

लेकिन यह काम करता है नहीं करता है, फ़ाइल एसडीकार्ड पर मौजूद है। मैंने गलती कहाँ की है?

+1

क्या कोई अपवाद फेंक दिया गया है? –

उत्तर

2
File f = new File(Environment.getExternalStorageDirectory(), "1.jpg"); 
String path = f.getAbsolutePath(); 
File f1 = new File(path); 

if(f1.exists()) { 
    Bitmap bmp = BitmapFactory.decodeFile(path); 
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); 
    WallpaperManager m=WallpaperManager.getInstance(this); 

    try { 
     m.setBitmap(bmp); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

ओपन AndroidManifest.xml फ़ाइल और जोड़ने की अनुमति की तरह ..

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

इस कोशिश करो और मुझे पता है कि क्या होने देना ..

5

शायद, आप स्मृति से बाहर चलाने अगर आपकी छवि है बड़े। आप लॉगकैट लॉग पढ़कर इसे सुनिश्चित कर सकते हैं। यदि ऐसा है, तो अपनी तस्वीर को डिवाइस आकार में कुछ हद तक समायोजित करने का प्रयास करें:

DisplayMetrics displayMetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    int height = displayMetrics.heightPixels; 
    int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width 

    // 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, width, height); 

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

    WallpaperManager wm = WallpaperManager.getInstance(this); 
    try { 
     wm.setBitmap(decodedSampleBitmap); 
    } catch (IOException e) { 
     Log.e(TAG, "Cannot set image as wallpaper", e); 
    } 
संबंधित मुद्दे