我是Android的新手抱歉......
我将所有照片保存在我的应用程序而不是SDCARD中。
我将此代码用于获取路径,但此代码返回String Array,我需要为我的类CustomSwipeAdpter返回Int Array,这个类用于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 :(得分:0)
您可以尝试按照方式而不是资源:
Bitmap bitmap = BitmapFactory.decodeFile(fileArray[position]);