0
所以我有一個片段,我有一個gridview的textView和一個按鈕。 在gridview中,我想顯示位於SD卡中的文件夾中的圖像。從gridview的SD卡文件夾Android片段加載圖像不起作用我
我添加的權限在清單:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
碎片的佈局看起來像這樣:
<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"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photogridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New capture"
android:id="@+id/btncapture"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tvCalea"
android:layout_alignTop="@+id/btncapture"
android:layout_toRightOf="@+id/btncapture"
android:layout_toEndOf="@+id/btncapture" />
</RelativeLayout>
的片段的代碼是:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String CaleaMea="";
Bundle bundle = this.getArguments();
if(bundle != null){
CaleaMea = bundle.getString("patu", "");
}
View view = inflater.inflate(R.layout.photos_layout,container,false);
GridView gridView = (GridView) view.findViewById(R.id.photogridview);
gridView.setAdapter(new PhotoImageAdapter(view.getContext(),CaleaMea)); // uses the view to get the context instead of getActivity().
Log.d("POZE frag:",CaleaMea);
TextView tv = (TextView) view.findViewById(R.id.tvCalea);
tv.setText(CaleaMea);
Button btn = (Button) view.findViewById(R.id.btncapture);
final String finalCaleaMea = CaleaMea;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File resultingFile = new File(finalCaleaMea.toString() + "/image.jpg");
Uri uriSavedImage=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(cameraIntent, 1888);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
和適配器:
public class PhotoImageAdapter extends BaseAdapter {
private Context mContext;
private String mCalea;
ArrayList<String> itemList = new ArrayList<String>();
public PhotoImageAdapter(Context c) {
mContext = c;
}
public PhotoImageAdapter(Context c, String calea) {
mContext = c;
mCalea = calea;
}
void add(String path) {
itemList.add(path);
}
void clear() {
itemList.clear();
}
void remove(int index){
itemList.remove(index);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220,
220);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public 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 = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height
/(float) reqHeight);
} else {
inSampleSize = Math.round((float) width/(float) reqWidth);
}
}
return inSampleSize;
}
AsyncTaskLoadFiles myAsyncTaskLoadFiles;
public class AsyncTaskLoadFiles extends AsyncTask<Void, String, Void> {
File targetDirector;
ImageAdapter myTaskAdapter;
public AsyncTaskLoadFiles(ImageAdapter adapter) {
myTaskAdapter = adapter;
}
@Override
protected void onPreExecute() {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
String targetPath = "";
targetPath = mCalea;
Toast.makeText(mContext,mCalea,Toast.LENGTH_SHORT).show();
targetDirector = new File(targetPath);
myTaskAdapter.clear();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
File[] files = targetDirector.listFiles();
for (File file : files) {
publishProgress(file.getAbsolutePath());
if (isCancelled()) break;
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
myTaskAdapter.add(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
myTaskAdapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
}
因此,該行爲是:沒有被顯示在片段,除了按鈕及TextView的。所以GridView沒有得到填充圖像...
我在做什麼錯了?
謝謝
你是什麼意思我應該聲明它是一個全球成員?我應該在哪裏申報?你的意思是,在公共類poze_fragment擴展片段{'後? – user1137313 2014-09-13 19:22:18
是的。這不是強制性的,但是當您需要在代碼的多個位置保留參考時,這是一個很好的做法。 – 2014-09-13 19:23:42
嘗試在適配器的getCount()方法內記錄列表大小,以確保其填充列表失敗。 – 2014-09-13 19:25:07