2014-09-13 56 views
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没有得到填充图像...

我在做什么错了?

谢谢

回答

1

你设置GridView的适配器,如:

gridView.setAdapter(new PhotoImageAdapter(view.getContext(),CaleaMea)); // uses the view to get the context instead of getActivity(). 

所以我觉得你不填充在其中的项目,因为你没有一个参考保持它。

编辑:为了确保这就是问题所在,你可以登录的项目你在你的适配器的getCount将获得()量方法。你也可以检查你是否在运行你的AsycTask,因为你的代码里没有引用“AsyncTask.execute(...)”方法。

尝试宣告适配器作为一个全球性的成员:

PhotoImageAdapter photoImageAdapter; 

然后intantiate它:

photoImageAdapter = new PhotoImageAdapter(view.getContext(),CaleaMea); 

创建适配器后您填充其中的项目,用你的AsyncTask:

AsyncTaskLoadFiles alf = new AsyncTaskLoadFiles(photoImageAdapter); 
alf.execute(); 

然后你设置gridview适配器:

gridView.setAdapter(photoImageAdapter); 

希望它有帮助。

+0

你是什么意思我应该声明它是一个全球成员?我应该在哪里申报?你的意思是,在公共类poze_fragment扩展片段{'后? – user1137313 2014-09-13 19:22:18

+0

是的。这不是强制性的,但是当您需要在代码的多个位置保留参考时,这是一个很好的做法。 – 2014-09-13 19:23:42

+0

尝试在适配器的getCount()方法内记录列表大小,以确保其填充列表失败。 – 2014-09-13 19:25:07