2016-05-09 2 views
0

Androidで新しいです。申し訳ありません....どのように私はパスリストフォルダのAndroidを取得し、Int配列に設定できます。

私は自分のアプリではなくSDCARDに写真を保存します。

私はパスを取得するためにこのコードを使用しますが、このコードは文字列配列を返します。このクラスのCustomSwipeAdpter(ページビュー用)をInt Int Arrayに返す必要があります。

  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}; 

私はこのイメージを設定する必要があります。

答えて

0

あなたは、リソースの道代わりに従ってみてくださいする可能性があります。

Bitmap bitmap = BitmapFactory.decodeFile(fileArray[position]); 
関連する問題