2016-05-09 31 views
0

我是Android新對不起....我該如何獲取路徑列表文件夾Android並在Int Array中設置。

我保存我的應用程序中的任何圖片不SDCARD。

我使用此代碼獲取路徑,但此代碼返回字符串數組,我需要返回Int數組爲我的類CustomSwipeAdpter,這個類用於Page View。

  ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
     // path to data/data/yourapp/app_data/imageDir 
     new_folder = cw.getDir(pasta, Context.MODE_PRIVATE); 

     ArrayList<String> arquivos = new ArrayList<String>(); 

     if (!new_folder.exists()){ 
      new_folder.mkdir(); 
     } 
     // verifica a pasta se tem arquivo // 
     File[] files = new_folder.listFiles(); 

     if ((files.length > 0)) { 
      String[] fileArray = new String[files.length]; 
      int[] fileArrayInt = new int[files.length]; 
      for (int i = 0; i < files.length; ++i) { 
       fileArray[i] = files[i].getAbsolutePath(); 
      } 
      //filesResource[i] = Integer.parseInt(files[i].getAbsolutePath()); 
     } 

我會在這個類在頁面視圖這個路徑,這個類是R.drawable.image01工作,我需要改變我的路徑...

public class CustomSwipeAdpter extends PagerAdapter { 

private int[] image_resources = {R.drawable.image01, R.drawable.image02, R.drawable.image03, 
     R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08}; 

private Context ctx; 
private LayoutInflater layoutInflater; 
private Resources resource; 

public CustomSwipeAdpter(Context ctx) { 
    this.ctx = ctx; 
    resource = ctx.getResources(); 
} 

@Override 
public int getCount() { 
    return image_resources.length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return (view == (LinearLayout) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position){ 
    layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false); 
    ImageView imageView = (ImageView) item_view.findViewById(R.id.imageView); 

    imageView.setImageBitmap(
      decodeSampledBitmapFromResource(resource, 
        image_resources[position], 
        1080, 
        2560)); 

    container.addView(item_view); 

    return item_view; 
} 


@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((LinearLayout) object); 
} 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
                int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 


public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 2; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 
    return inSampleSize; 
} 

這個類是罰款當我使用:

private int[] image_resources = {R.drawable.image01, R.drawable.image02, R.drawable.image03, 
     R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08}; 

我需要設置此image_resources我的路徑。

回答

0

願你可以嘗試遵循的方式,而不是資源:

Bitmap bitmap = BitmapFactory.decodeFile(fileArray[position]); 
相關問題