上载图像从本地系统(PC - 文件系统)服务器 [英] Uploading a image from a local system( PC - file system ) to a server

查看:119
本文介绍了上载图像从本地系统(PC - 文件系统)服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么?

目前我使用的形象从我绘制的图像后到服务器

currently i am using the image from my drawable to POST the image to a server

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image); 


什么,我想干什么?

  • 现在,对点击图像的图像认为我应该能够选择 从我的桌面图像(我正在我的模拟器)
  • 图片我需要从 C选择:\图片\ ,所有的样本图像 位于
  • Now for the image view on click of image i should be able to select the image from my desktop(I am running my emulator)
  • Images i need to select from C:\images\ where all the sample images are located

注意 ::图片的位置,我不是指到SD卡....我指的是一个文件夹在我的电脑

Note:: Image location, I am not referring to SD card .... I am referring to a folder in my PC

MainActivity.java

public class MainActivity extends Activity {

    Button submit;
    ProgressDialog pDialog;
    InputStream is;

    EditText name;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);

        name = (EditText) findViewById(R.id.editText1);
        imageView = (ImageView) findViewById(R.id.imageView1);

        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new MainTest().execute();


            }
        });
    }



    /**
     * Method to post the image to the server.
     * U will have to change the url which will accept the image data.
     * @throws IOException 
     */
    public void postImageData() {


        try
        {

            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
                reqEntity.addPart("key", bab);
                reqEntity.addPart("key1", new StringBody(name.getText().toString()));
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            postRequest.setEntity(reqEntity);       
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
        }catch(Exception e){
            e.getStackTrace();
        }


    }
    public class MainTest extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            postImageData();

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // data=jobj.toString();
            pDialog.dismiss();

        }

    }

}


activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="168dp"
        android:layout_weight="0.41"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="101dp"
            android:layout_marginTop="32dp"
            android:clickable="false"
            android:src="@drawable/image" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="195dp"
            android:layout_height="wrap_content"
            android:paddingTop="50dp"
            android:layout_gravity="center"
            android:ems="10" />

    </LinearLayout>

    <Button
        android:id="@+id/SUBMIT_BUTTON_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="47dp"
        android:text="SUBMIT" />

</LinearLayout>


在实现这一目标的任何想法


Any ideas on achieving this

感谢

推荐答案

您有两个选择这种

1>建立你自己的服务器在您的PC,使其加载所有图像和图像的URI可用的,并REST API用于检索它从你的电脑的所有图像通过JSON的任何地方。这样的形象可以在你的手机中加载,可以共享,上传过任何were.For例如,你可以使用<一个href="http://stackoverflow.com/questions/20561478/using-inputstream-with-multipartentitybuilder-apache-error">this方法上载所选择的图像。

1>Build your own server in your PC such that it load all images and image URI available for it, and make REST api for it retrieve all images from your pc to any place via JSON. such that image can be loaded in your mobile, can shared, uploaded any were.For example you can use this method to upload the selected image.

2>简单的方法:

使用 aFilechooser librery在您的项目,这样你可以直接选择从<一个影像/文件href="http://www.freewaregenius.com/five-ways-to-transfer-files-between-pc-and-android-wirelessly-including-some-that-youve-never-heard-of/"相对=nofollow>第三方应用(例如:ES文件浏览器),它允许您选择从您的电脑文件,并允许从您的PC上的任何文件,图像上传到你的服务器。用这种方法唯一的问题是,你必须从其他第三方应用程序查看图像,文件!如果愿意,你可以使用谷歌驱动器同步机制,同步所有图像文件夹从您的PC或使用谷歌API

Use aFilechooser librery in your project such that you can directly choose the image/file from the 3rd party apps (ex:ES File Explorer) which allow you to select the file from your pc and which allow to upload any file image from your pc to your server. only problem with this method is you have to view the images , files from other third party app! If want you can use google Drive sync mechanism to sync all image folder from your pc or Use Google API.

这篇关于上载图像从本地系统(PC - 文件系统)服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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