2011-11-14 79 views
0

我打算開發一個Android應用程序,其中客戶將能夠更改應用程序背景並將其個人圖像或照片設置爲背景。但這意味着圖像不會出現在「drawables」中,我對android非常陌生,我一直在爲此尋找很長的時間,請任何人都幫助我。爲Android應用程序設置自定義背景

回答

0

嘗試......

public class GalleryAdapter extends Activity { 
     private int count; 
     private Bitmap[] thumbnails; 
     private boolean[] thumbnailsselection; 
     private String[] arrPath; 
     private ImageAdapter imageAdapter; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.image_selector); 
      final String[] columns = { MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID }; 
      final String orderBy = MediaStore.Images.Media._ID; 
      Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null,orderBy); 
      int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); 
      this.count = imagecursor.getCount(); 
      this.thumbnails = new Bitmap[this.count]; 
      this.arrPath = new String[this.count]; 
      this.thumbnailsselection = new boolean[this.count]; 

      for (int i = 0; i < this.count; i++) { 
       imagecursor.moveToPosition(i); 
       int id = imagecursor.getInt(image_column_index); 
       int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); 
       thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
       getApplicationContext().getContentResolver(), id, 
       MediaStore.Images.Thumbnails.MICRO_KIND, null); 
       arrPath[i] = imagecursor.getString(dataColumnIndex); 
      } 

      GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); 
      imageAdapter = new ImageAdapter(); 
      imagegrid.setAdapter(imageAdapter); 
      imagecursor.close(); 
      final Button selectBtn = (Button) findViewById(R.id.selectBtn); 
      selectBtn.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        final int len = thumbnailsselection.length; 
        int cnt = 0; 
        String selectImages = ""; 
        for (int i = 0; i < len; i++){ 
         if (thumbnailsselection[i]) { 
          cnt++; 
          selectImages = selectImages + arrPath[i] + "|"; 
         } 
        } 

        if (cnt == 0) { 
         Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show(); 
        } else { 
         Toast.makeText(getApplicationContext(), "You've selected Total " + cnt + " image(s).",Toast.LENGTH_LONG).show(); 
         Log.d("SelectedImages", selectImages); 
        } 
       } 
      }); 
     } 

     public class ImageAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 
      public ImageAdapter() { 
       mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 

      public int getCount() { 
       return count; 
      } 
      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.gallery_item,null); 
        holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); 
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox); 
        convertView.setTag(holder); 

       } else { 
        holder = (ViewHolder) convertView.getTag(); 
       } 
       holder.checkbox.setId(position); 
       holder.imageview.setId(position); 
       holder.checkbox.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         CheckBox cb = (CheckBox) v; 
         int id = cb.getId(); 
         if (thumbnailsselection[id]) { 
          cb.setChecked(false); 
          thumbnailsselection[id] = false; 
         } else { 
          cb.setChecked(true); 
          thumbnailsselection[id] = true; 
         } 
        } 
       }); 
       holder.imageview.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         int id = v.getId(); 
         Intent intent = new Intent(); 
         intent.setAction(Intent.ACTION_VIEW); 
         intent.setDataAndType(Uri.parse("file://" + arrPath[id]),"image/*"); 
         startActivity(intent); 
        } 
       }); 
       holder.imageview.setImageBitmap(thumbnails[position]); 
       holder.checkbox.setChecked(thumbnailsselection[position]); 
       holder.id = position; 
       return convertView; 
      } 
     } 
     class ViewHolder { 
      ImageView imageview; 
      CheckBox checkbox; 
      int id; 
     } 

} 

image_selector.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"> 
    <Button android:id="@+id/selectBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Select" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:minWidth="200px" /> 
    <GridView android:id="@+id/PhoneImageGrid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:columnWidth="90dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
     android:layout_above="@id/selectBtn" /> 
</RelativeLayout> 

gallery_item.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"> 
    <ImageView android:id="@+id/thumbImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 
    <CheckBox android:id="@+id/itemCheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 
+0

嗨感謝您的回覆。我有一個「更改背景按鈕點擊它,我應該能夠瀏覽圖像。 – Antu

+0

現在檢查,我改變了答案..! – Noby

+0

對不起,你錯了,但我新來這個我認爲幾個包丟失,說持有者 – Antu

相關問題