Android将图片从图库上传到服务器 [英] Android upload image from gallery to server

查看:168
本文介绍了Android将图片从图库上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像从Androids照片库上传到服务器。我所做的所有沟通都是使用Object Streams,但现在我不确定如何做到这一点。哦,我使用输入流下载图像,您可以使用URL直接指向图像。如果有人能指出我正确的方向,将不胜感激。

I am trying to upload an image from the Androids photo gallery to a server. All the communication I have done has been with Object Streams but now I am unsure as to how I would do this. Oh and I have used an Input Stream to download an image where you point directly to the image using an URL. If someone could point me in the right direction would be appreciated.

谢谢

推荐答案

要从应用程序上传图像到服务器,您可以按照以下教程进行操作:

For uploading images to a server from your application you can follow following tutorials:


  1. 使用Android上的POST将文件上传到HTTP服务器。

使用http POST多部分上传图片或文件。

以上两个网址将向您解释如何将图片从您的应用程序上传到服务器。

The above two url will explain you how to upload images from your application to server.

要从照片库上传图片,您需要该图像文件的路径,并将获取的路径替换为 / data / file_to_sen d.mp3 在第一个网址中。

For uploading image from your photo gallery you require the path of that image file and replace the obtained path with /data/file_to_send.mp3 in first url.

要从移动图库获取图像的路径,您可以按照以下代码:

To obtain path of the image from the mobile gallery you can follow the following code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button)findViewById(R.id.Button01);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                openGallery(SELECT_FILE1);
            }
        });
    }

    public void openGallery(int req_code) {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,
                               "Select file to upload "), req_code);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();

            if (requestCode == SELECT_FILE1) {
                selectedPath1 = getPath(selectedImageUri);
                System.out.println("selectedPath1 : " + selectedPath1);
            }

            if (requestCode == SELECT_FILE2) {
                selectedPath2 = getPath(selectedImageUri);
                System.out.println("selectedPath2 : " + selectedPath2);
            }

            tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
        }
    }

    public String getPath(Uri uri) {

        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        return cursor.getString(column_index);
    }

下载图片时,您可以执行以下代码。

for downloading images you can do the following code.

    ImageView image = (ImageView)findViewById(R.id.image);
    if(!ImageUrl.equals("no image")) {          
        try {
            image.setImageDrawable(grabImageFromUrl(ImageUrl));

        } catch(Exception e) {     
          }  
    } 

    private Drawable grabImageFromUrl(String url) throws Exception {
          return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }

这篇关于Android将图片从图库上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆