如何将相机拍摄的照片添加到GridView

时间:2017-04-19 10:10:58

标签: android image gridview camera

在我的应用程序中我拍照并保存,现在我想在gridView中显示它并重复这个过程来构建我拍摄的一些画廊照片。它应该看起来像(第一个屏幕): enter image description here

所以一开始这个画廊应该是空的然后我会填补它。

这是我已经得到的东西

public class GuidePhotoAlbum extends Activity {

    private static final int TAKE_PICTURE = 1;
    Button button1;
    private Uri imageUri;
    GridView gridview;
    Bitmap help1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide_photo_album);
        gridview = (GridView) findViewById(R.id.gridView);
        button1 = (Button)findViewById(R.id.buttonCam);
        //imageView = (ImageView)findViewById(R.id.imageView) ;

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TakePictureIntent();
            }
        });
    }

    private void TakePictureIntent()
    {
        int count = 0;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +"v" +count+".jpg"));
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(takePictureIntent, TAKE_PICTURE);
        count++;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == TAKE_PICTURE)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                try {
                    help1 = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);
                    //imageView.setImageBitmap(thumbnail.extractThumbnail(help1,help1.getWidth(),help1.getHeight()));
                    gridview.setAdapter(new ImageAdapter(this));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
            super.onActivityResult(requestCode, resultCode, data);
    }

    public class ImageAdapter extends BaseAdapter
    {

        public ImageAdapter(GuidePhotoAlbum guidePhotoAlbum) {

        }

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }

我不知道该在ImageAdapter中应该做什么来添加这张图片并显示它。还有一件事如果会有大量的图像,它应该滚动。

这里的XML到

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/guide_photo_album"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kylu.layout.GuidePhotoAlbum">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0sp"
        android:layout_weight="3">


       <GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/gridView"
            android:numColumns="3"
            android:horizontalSpacing="5dp"
            android:verticalSpacing="5dp"
            android:stretchMode="columnWidth">

        </GridView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="0sp"
        android:layout_weight="1">
        <Button
            android:text="Take Picture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="onClick"
            android:id="@+id/buttonCam" />
    </LinearLayout>
    </LinearLayout>
</RelativeLayout>

P.S抱歉英语不好

1 个答案:

答案 0 :(得分:0)

通过以下方法获取相机拍摄的所有图像

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<MyConfiguration>(Configuration.GetSection("MyConfiguration"));
  services.AddSingleton<IProvider>(new ProviderOne(//configuration)); //here is the problem
}

}

像这样改变你的imageAdpater construtor

public void List<String> getCameraImages(Context context) {
final String[] projection = { MediaStore.Images.Media.DATA };
final String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
final String[] selectionArgs = { CAMERA_IMAGE_BUCKET_ID };
final Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, 
        projection, 
        selection, 
        selectionArgs, 
        null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());
if (cursor.moveToFirst()) {
    final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    do {
        final String data = cursor.getString(dataColumn);
        result.add(data);
    } while (cursor.moveToNext());
}
cursor.close();
return result;

并将以下代码放在imageAdapter的getview方法中

public ImageAdapter(Context context,List<String>list) {
     this.imageList = list;
    }