简体   繁体   English

如何将SD卡中的图像加载到CoverView中

[英]How do I load an image from sd card into coverview

I am doing an application which consists of a cover flow. 我正在做一个包含封面流程的应用程序。 Currently I am trying to make the cover flow images be obtained from the sd card. 目前,我正在尝试从SD卡获取封面流图像。 But I am not exactly sure how should I display the image. 但是我不确定应该如何显示图像。

this is the image adapter: 这是图像适配器:

    public class ImageAdapter extends BaseAdapter {

     int mGalleryItemBackground;
     private Context mContext;

     private FileInputStream fis;
     private Integer[] mImageIds = {

       //Instead of using r.drawable, 
         i need to load the images from my sd card instead

             R.drawable.futsing,
             R.drawable.futsing2

     };

     private ImageView[] mImages;

     public ImageAdapter(Context c) {
      mContext = c;
      mImages = new ImageView[mImageIds.length];
     }

     public int getCount() {
         return mImageIds.length;
     }

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

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


     @TargetApi(8)
    public View getView(int position, View convertView, ViewGroup parent) {

         int screenSize = getResources().getConfiguration().screenLayout &
         Configuration.SCREENLAYOUT_SIZE_MASK;

         ImageView i = new ImageView(mContext); 
         i.setImageResource(mImageIds[position]);

         switch(screenSize) {
         case Configuration.SCREENLAYOUT_SIZE_XLARGE:
         //Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
         Display display4 = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
         int rotation4 = display4.getRotation();
         Log.d("XLarge:",String.valueOf(rotation4));

         /** LANDSCAPE **/
         if(rotation4 == 0 || rotation4 == 2) 
         {

         i.setLayoutParams(new CoverFlow.LayoutParams(300, 300));
         i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
         drawable.setAntiAlias(true);
         return i;
         }

         /** PORTRAIT **/
         else if (rotation4 == 1 || rotation4 == 3) 
         {
             i.setLayoutParams(new CoverFlow.LayoutParams(650, 650));
             i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
             BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
             drawable.setAntiAlias(true);
             return i;
         }
         break;

     default:
 }
        return null;

     }
   /** Returns the size (0.0f to 1.0f) of the views 
      * depending on the 'offset' to the center. */ 
      public float getScale(boolean focused, int offset) { 
        /* Formula: 1 / (2 ^ offset) */ 
          return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
      } 

 }

and this is how i download and save the file. 这就是我下载和保存文件的方式。 I need to use the image saved and upload it into my coverflow 我需要使用保存的图像并将其上传到我的Coverflow中

 // IF METHOD TO DOWNLOAD IMAGE
void downloadFile() {

    new Thread(new Runnable(){  // RUN IN BACKGROUND THREAD TO AVOID FREEZING OF UI
    public void run(){  
            Bitmap bmImg;
            URL myFileUrl = null;
            try {
                //for (int i = 0; i < urlList.size(); i ++)
                //{
                //url = urlList.get(i);
                myFileUrl = new URL("http://static.adzerk.net/Advertisers/d18eea9d28f3490b8dcbfa9e38f8336e.jpg");    // RETRIEVE IMAGE URL
                //}
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream in = conn.getInputStream();
                Log.i("im connected", "Download");
                bmImg = BitmapFactory.decodeStream(in);

                saveFile(bmImg);
            } catch (IOException e) {
                e.printStackTrace();
            }}   
    }).start(); // START THREAD

    }

    // SAVE THE IMAGE AS JPG FILE
private void saveFile(Bitmap bmImg) {
    File filename;
try {
     // GET EXTERNAL STORAGE, SAVE FILE THERE
     File storagePath = new File(Environment.getExternalStorageDirectory(),"Covers");
     storagePath.mkdirs();
    filename = new File(storagePath + "/image.jpg");
    FileOutputStream out = new FileOutputStream(filename);
    bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);

    out.flush();
    out.close();
    MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(),
            filename.getName());

    // ONCE THE DOWNLOAD FINISHES, CLOSE DIALOG
   Toast.makeText(getApplicationContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
    e.printStackTrace();
}   

}

for loading images from Sd card to ImageView change your code as: 用于将图像从SD卡加载到ImageView的代码更改为:

put images path in array : 将图像路径放在array中:

 String strpath=Environment.getExternalStorageDirectory();
     private String[] mImageIds = {

             strpath+"a.jpg",
             strpath+"b.jpg",
             strpath+"c.jpg",
             .....
     };

and for setting in getView to ImageView you will need to create Drawable : 并将getView设置为ImageView,您将需要创建Drawable:

ImageView i = new ImageView(mContext); 
i.setImageDrawable(Drawable.createFromPath(mImageIds[position]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM