将图像从GalleryView上传到PHP Server

时间:2014-05-30 06:40:50

标签: android android-intent android-gallery androidhttpclient galleryview

我是Android开发的新手。我正在尝试将android-gallery图像发送到服务器。我从相机拍摄照片并在画廊视图中显示拍摄的图像。现在我需要将这些图像发送到服务器数据库。我不知道这样做。听到我的代码到现在为止。

   <Gallery
    android:id="@+id/gallery"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#55000000"
    android:gravity="center_vertical"
    android:spacing="16dp" />

MainActivity

public class MainActivity extends Activity implements
    AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

Button b;
public static int count = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

    b = (Button) findViewById(R.id.btnSelectPhoto);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            selectImage();
        }
    });

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this, ReadSDCard()));
    g.setOnItemSelectedListener(this);
}

private void selectImage() {
    // TODO Auto-generated method stub

    final CharSequence[] options = { "Take Photo", "Choose from Gallery",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setTitle("Add Photo!");

    builder.setItems(options, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo"))

            {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                final String dir = (android.os.Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolderrr/");
                File f = new File(dir);
                f.mkdir();
                count++;

                Uri uriSavedImage = Uri.fromFile(new File(dir + count
                        + ".jpg"));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                startActivityForResult(intent, 1);

            }

            else if (options[item].equals("Choose from Gallery"))

            {

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(intent, 2);

            }

            else if (options[item].equals("Cancel")) {

                dialog.dismiss();

            }

        }

    });

    builder.show();

}

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
        long id) {
    /* mSwitcher.setImageResource(mImageIds[position]); */
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    return i;
}

private List<String> ReadSDCard() {
    List<String> tFileList = new ArrayList<String>();

    // It have to be matched with the directory in SDCard
    File f = new File("/storage/sdcard0/Pictures/picFolderrr/");

    File[] files = f.listFiles();

    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        /* It's assumed that all file in the path are in supported type */
        tFileList.add(file.getPath());
    }
    return tFileList;
}

/* private ImageSwitcher mSwitcher; */

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private List<String> FileList;

    public ImageAdapter(Context c, List<String> fList) {
        mContext = c;
        FileList = fList;

    }

    public int getCount() {
        return FileList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        Bitmap bm = BitmapFactory.decodeFile(FileList.get(position)
                .toString());
        Bitmap re = Bitmap.createScaledBitmap(bm, 60, 60, false);
        i.setImageBitmap(re);

        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        i.setBackgroundResource(R.drawable.picture_frame);

        return i;
    }
}

}

1 个答案:

答案 0 :(得分:0)

此代码将数据(图像,mp3,文本文件等)上传到HTTP服务器

Uploading files to HTTP server using POST on Android.