2012-06-18 4 views
0

im new для android, и я пытался выяснить, как отображать фотографию на Android. Я искал всю неделю, не повезло и вы хотите знать, есть ли какие-либо андроида образец кода или примеры о том, как установить обои для приложения из галереи изображенийandroid app обои из галереи

спасибо всем

+0

[1] - это несколько связанный с этим вопрос, к которому вы можете обратиться. [1]: http://stackoverflow.com/questions/3035050/setting-wallpaper-through-code –

ответ

0

Используйте следующий метод для выбора изображения из изображения Gallary

//Declare private static final int PICK_IMAGE = 1; 

    try { 
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 
      } 

А на активность Результат Запись:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch (requestCode) { 
    case PICK_IMAGE: 

     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      try { 
       // OI FILE Manager 
       String filemanagerstring = selectedImageUri.getPath(); 

       // MEDIA GALLERY 
       String selectedImagePath = getPath(selectedImageUri); 

       if (selectedImagePath != null) { 
        filePath = selectedImagePath; 
       } else if (filemanagerstring != null) { 
        filePath = filemanagerstring; 
       } else { 
        Toast.makeText(getApplicationContext(), "Unknown path", 
          Toast.LENGTH_LONG).show(); 

       } 

       if (filePath != null) {   

        decodeFile(filePath); 
       } else { 
        bitmap = null; 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "Internal 
        error",Toast.LENGTH_LONG).show(); 

      } 
     } 
     break; 
    default: 
    } 



} 


    //Decode file() where you will get decoded file and then you can use that image 
     //file according to your requirement 

    public void decodeFile(String filePath) { 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    bitmap = BitmapFactory.decodeFile(filePath, o2); 

    image.setImageBitmap(bitmap);// set ImageView in your case set Layout 
       //background 

} 
-1

пожалуйста Проверьте код.

Bitmap mBitmap = BitmapFactory.decodeResource("imageID"); 

       WallpaperManager myWallpaperManager = WallpaperManager 
         .getInstance(getApplicationContext()); 

       try { 
        myWallpaperManager.setBitmap(mBitmap); // here your image bitmap. 
        Toast.makeText(SetWallPaper.this, "Wallpaper set", 
          Toast.LENGTH_SHORT).show(); 
       } catch (IOException e) { 
        Toast.makeText(SetWallPaper.this, 
          "Error setting wallpaper", Toast.LENGTH_SHORT) 
          .show(); 
       } 

Я думаю, это вам поможет.

0

Попробуйте найти новый сайт бостон для андроид учебники развития, я думаю, тут 41 и 42 учит, как получить изображение из галереи или с камеры, и установить его в качестве обоев. Попробуйте эту ссылку http://thenewboston.org/list.php?cat=6. Счастливое кодирование :)

Смежные вопросы