2013-11-04 11 views
0

늘 그렇듯이, 저는 이걸 가지고 머리를 쓰고 있습니다.안드로이드는 gridview 이미지에서 배경 화면을 설정합니다

내가 성공적으로의 GridView에 표시 할 SDCard에의 폴더에서 이미지의 집합을 얻을 수있는 튜토리얼을 수정 한

내가 무엇을 시도하고, 사용자가 배경 화면으로 설정 하나를 선택할 수있다 .

내 문제는 내가 이미지의 폴더 위치가 나타나야 수신되었음을 알았 기 때문에 그것을 얻는 방법을 찾지 못하고 이후에 배경 화면으로 설정할 수 있습니다.

나는 많은 시도 중 하나의 작은 부분을 포함했지만 galleryitem.xml 파일에서 ImageView에 액세스하려고 할 때 주로 NullPointers를 비롯한 모든 오류가 발생합니다.

public class MyWallpapers extends Activity { 


private boolean[] thumbnailsselection; 
private String[] arrPath; 
private ImageAdapter imageAdapter; 

ImageView Selected; 


ArrayList<String> f = new ArrayList<String>();// list of file paths 
File[] listFile; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.wallpaper_view_activity); 


    getFromSdcard(); 
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); 
    imageAdapter = new ImageAdapter(); 
    imagegrid.setAdapter(imageAdapter); 


    final ImageView Selected = (ImageView) findViewById(R.id.thumbImage); 
     Selected.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 


       //need to get the position of the file and set it as wallpaper when the user clicks the image 

       Context context = this.getAbsolutePath(); 
       Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), 
         mImageIds[pos]); 
       context.setWallpaper(context); 

      } 
     }); 
    } 



public void getFromSdcard() 
{ 
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"Wallpaper"); 

     if (file.isDirectory()) 
     { 
      listFile = file.listFiles(); 


      for (int i = 0; i < listFile.length; i++) 
      { 

       f.add(listFile[i].getAbsolutePath()); 

      } 
     } 
} 

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return f.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(
        R.layout.galleryitem, null); 
      holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); 

      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 


     Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position)); 
     holder.imageview.setImageBitmap(myBitmap); 
     return convertView; 
    } 
} 
class ViewHolder { 
    ImageView imageview; 


} 
    } 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/thumbImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" /> 

이에 어떤 도움이 될 것 wallpaper_view_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<GridView 
    android:id="@+id/PhoneImageGrid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/button1" 
    android:columnWidth="90dp" 
    android:gravity="center" 
    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="10dp" /> 

그리고 마지막으로 galleryitem.xml 내의 GridView입니다 , a 언제나, 크게 감사드립니다.

답변

0
ImageLocation = listFile[position].getAbsolutePath(); 

          BitmapFactory.Options options = new BitmapFactory.Options(); 
          options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = BitmapFactory.decodeFile(ImageLocation, options); 


          WallpaperManager myWallpaperManager 
          = WallpaperManager.getInstance(getApplicationContext()); 
          try { 
           myWallpaperManager.setBitmap(bitmap); 
           Toast.makeText(MyWallpapers.this, "Wallpaper Set", Toast.LENGTH_SHORT).show(); 
          } catch (IOException e) { 
           Toast.makeText(MyWallpapers.this, "Error", Toast.LENGTH_SHORT).show(); 
           e.printStackTrace(); 
          } 
관련 문제