我正在尝试将图片从
Androids照片库上传到服务器.我所做的所有的沟通都是与Object Streams进行的,但现在我不确定我该怎么做.哦,我已经使用输入流来下载一个图像,您直接使用URL直接指向图像.如果有人能指出正确的方向,将不胜感激.
谢谢
解决方法
要从应用程序将图像上传到服务器,您可以按照以下教程:
> Uploading files to HTTP server using POST on Android.
> Upload image or file using http POST multi-part.
以上两个网址将解释您如何将图像从应用程序上传到服务器.
要从照片库上传图像,您需要该图像文件的路径,并将获取的路径替换为第一个URL中的/data/file_to_send.mp3.
@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); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
为了下载图像,您可以执行以下代码.
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"); }