2012-08-14 12 views
5

मैं एक छवि गैलरी है कि मेरे कोड है, लेकिन काम नहीं कर रहा एक url..this से चित्रों को प्राप्त बनाना चाहेंगे ...एंड्रॉइड में यूआरएल से गैलरी छवियां?

public class ImagesActivity extends Activity 
{  

private String[] imageIDs= {"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg", 
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg", 
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg", 
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg" 
}; 

@Override  
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.displayview); 

    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 

    gallery.setAdapter(new ImageAdapter(this));   
    gallery.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView parent, View v, int position, long id) 
     {     
//        Toast.makeText(getBaseContext(), "pic" + (position + 1) + " selected", Toast.LENGTH_SHORT).show(); 
          //---display the images selected--- 
          ImageView imageView = (ImageView) findViewById(R.id.image1);     
          imageView.setImageResource(imageIDs[position]); 
     } 
    }); 

} 


public class ImageAdapter extends BaseAdapter 
{ 
    private Context context; 
    private int itemBackground; 

    public ImageAdapter(Context c) 
    { 
     context = c; 
     //---setting the style--- 
     TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
     itemBackground = a.getResourceId(
      R.styleable.Gallery1_android_galleryItemBackground, 0); 
     a.recycle();      
    } 

    //---returns the number of images--- 
    public int getCount() { 
     return imageIDs.length; 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) { 
     return position; 
    }    

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

    //---returns an ImageView view--- 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(context); 
     imageView.setImageResource(imageIDs[position]); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
     imageView.setBackgroundResource(itemBackground); 
     return imageView; 
    } 
}  

} 

ViewsActivity.java

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

    startActivity(new Intent(this, ImagesActivity.class)); 

    } 
} 

त्रुटि है कि है मैं imageView.setImageResource (imageIDs [स्थिति]) का उपयोग नहीं कर सकता; तारों के लिए ... चींटी हेप्ल कृपया?

+1

आप URL से चित्र प्राप्त करने की आवश्यकता है और फिर के रूप में 'ImageResource' छवि सेट? – Doomsknight

+0

यूआरएल से छवि नहीं मिल रहा है – Kumar

+0

उत्तर देखें। दोनों कार्य। – Doomsknight

उत्तर

9

उदाहरण के लिए आप लिंक से चित्र को डीकोड करने की जरूरत है,

URL url = new URL("load your URL"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
img_downloaded.setImageBitmap(bmp); 

अद्यतन:

मैं अपना कोड संशोधित किया, आप कर सकते हैं इसके साथ प्रयास करें,

public class MainActivity extends Activity { 

    private String[] imageIDs = { 
      "http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg", 
      "http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg", 
      "http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg", 
      "http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg" }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Gallery gallery = (Gallery) findViewById(R.id.gallery1); 

     gallery.setAdapter(new ImageAdapter(this)); 
     gallery.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, int position, 
        long id) { 
       // Toast.makeText(getBaseContext(), "pic" + (position + 1) + 
       // " selected", Toast.LENGTH_SHORT).show(); 
       // ---display the images selected--- 
       ImageView imageView = (ImageView) findViewById(R.id.imageview1); 
       URL url = null; 
       try { 
        url = new URL(imageIDs[position]); 
        Bitmap bmp = null; 
        try { 
         bmp = BitmapFactory.decodeStream(url 
           .openConnection().getInputStream()); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        imageView.setImageBitmap(bmp); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 
     }); 

    } 

    public class ImageAdapter extends BaseAdapter { 
     private Context context; 
     private int itemBackground; 

     public ImageAdapter(Context c) { 
      context = c; 
      // ---setting the style--- 
      TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
      itemBackground = a.getResourceId(
      R.styleable.Gallery1_android_galleryItemBackground, 0); 
      a.recycle(); 
     } 

     // ---returns the number of images--- 
     public int getCount() { 
      return imageIDs.length; 
     } 

     // ---returns the ID of an item--- 
     public Object getItem(int position) { 
      return position; 
     } 

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

     // ---returns an ImageView view--- 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView imageView = new ImageView(context); 
      // imageView.setImageResource(imageIDs[position]); 
      URL url; 
      try { 
       url = new URL(imageIDs[position]); 
       Bitmap bmp = BitmapFactory.decodeStream(url.openConnection() 
         .getInputStream()); 
       imageView.setImageBitmap(bmp); 
       imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
       imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
       imageView.setBackgroundResource(itemBackground); 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return imageView; 
     } 
    } 

} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Gallery 
     android:id="@+id/gallery1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/imageview1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/gallery1" /> 


</RelativeLayout> 
+0

हम्म हमारे उत्तरों को देखो देखो! +1 – Doomsknight

+0

@ डूमस्कनाइट: हाँ: पी – Aerrow

+0

आउट इस पाठ को केवल (सैन फ्रांसिस्को की छवियां) – Kumar

5

getView विधि में:

URL newurl = new URL(imageIDs[position]); //Your URL String. 
Bitmap image = BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
imageView.setImageBitmap(image); 
+0

लेकिन – Kumar

+0

आने में त्रुटि – Kumar

+0

के साथ घूमने से पूछने के बाद इसे एक कोशिश और पकड़ने के खंड से घिरा हुआ है। :) अपने कोड के एरोस संशोधन देखें, जिसमें यह शामिल है। – Doomsknight

3

इस गैलरी छवि में उपयोग करने वाली url उत्तर Click Link

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