Android 如何将同步的外部应用程序映像正确发送到RESTful WCF

Android 如何将同步的外部应用程序映像正确发送到RESTful WCF,android,Android,我是Android的新手,我正在尝试将一些图像从Android发送到RESTFul WCF 现在我可以从图库中选择图像并将其发送到服务器 WCF希望图像为流 但我对储存在平板电脑中的同步图像(如Facebook或G+照片)有问题。(我不知道它们是缓存还是什么) 我使用这个函数来获取图像的路径 public static String getRealPathFromURI(Context context, Uri contentUri) { String path = null;

我是Android的新手,我正在尝试将一些图像从Android发送到RESTFul WCF

现在我可以从图库中选择图像并将其发送到服务器

WCF希望图像为

但我对储存在平板电脑中的同步图像(如Facebook或G+照片)有问题。(我不知道它们是缓存还是什么)

我使用这个函数来获取图像的路径

public static String getRealPathFromURI(Context context, Uri contentUri) {
         String path = null;
         if (contentUri.getScheme().toString().compareTo("content")==0)
         {
            String[] proj = { MediaStore.Images.Media.DATA };           
            Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
            cursor.moveToFirst();
            path = cursor.getString(column_index);
         }
         else

         {
             path = contentUri.getPath();
         }
         Log.i(TAG, path);
         return path;
        }
通过这种图像,我可以获得一条类似于以下内容的互联网路径:

只是为了澄清和评论。我得到了一个“内容”方案。。所以我从“如果”中得到了路径,比如:

content://com.sec.android.gallery3d.provider/picasa/item/5703464571893262194
要将其发送到服务器,我使用MultipartEntity,因为我在其他帖子中看到了这样做,如下所示:

 ((MultipartEntity) oInputEntity).addPart(
                            "fileContents",
                            new FileBody(new File(Utilities.getRealPathFromURI(context,
                                            imageUri)),

 "image/jpeg"));
对于这类图像,我得到了一个
FileNotFoundEception
我想这是因为图像路径是一个Internet路径,所以多方不知道如何检索它

因此,我改变了下载图像的方法,现在正在使用此代码

public static File getFileFromURI(Context context, Uri contentUri) {
             String path = IntUtilities.getRealPathFromURI(context, contentUri);
             Log.i(TAG, path);
             final File file;
             if (path.startsWith("http") || path.startsWith("/http") )
             {
                 //if its an image form internet lets download it and save it in our directory and return that file
                // Determine Uri of camera image to save.
                final String fname = "BIR" + UUID.randomUUID() + ".jpg";
                final File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "BIR");
                root.mkdirs();
                file = new File(root, fname);    


                try {
                     final URL url = new URL(path);
                     final HttpURLConnection urlConnection = (HttpURLConnection) url
                             .openConnection();
                     urlConnection.setRequestMethod("GET");
                     urlConnection.setDoOutput(false);
                     urlConnection.connect();
                     final FileOutputStream fileOutput = new FileOutputStream(file);
                     final InputStream inputStream = urlConnection.getInputStream();

                     @SuppressWarnings("unused")
                    int downloadedSize = 0;

                     byte[] buffer = new byte[1024];
                     int bufferLength = 0;
                     while ((bufferLength = inputStream.read(buffer)) > 0) {
                         fileOutput.write(buffer, 0, bufferLength);
                         downloadedSize += bufferLength;
                     }
                     // close the output stream when done
                     fileOutput.close();
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
             else
             {          
                 file = new File(path);
             }
             return file;
            } 

但我对这个解决方案不满意,似乎是加倍努力,我在平板电脑上关闭了Wifi和3g,也在平板电脑上关闭了,我自己也在平板电脑上看到了这些图像,所以我猜它们在第一次同步时被本地复制或缓存在平板电脑上。我在连接到我的计算机时(在Windows资源管理器中)查找了它们,以查看它们是否在那里,但我没有看到它们,可能是我做错了什么,或者不知道存储文件夹

我不喜欢这个解决方案的主要原因是,如果你现在没有互联网,显然图像不会被下载,我正在制作的应用程序应该可以离线工作,而且。。图像就在那里,不应该向互联网请求猜测本地图像

话虽如此,有没有办法找到同步照片的真实/物理路径,这些照片使用http或https方案使用多协议发送这些图像

或者用另一种合适的方式将这些图像发送到服务器


非常感谢您的帮助

从选择器对话框中,您总能获得位图

选择器-->$Result->getData()=imageUri

从imageUri中,通过运行以下代码获取位图:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
一旦你得到位图

您可以将其放入文件链接

可以使用Hashmap将其缓存在内存中

mBitMap = BitmapFactory.decodeStream(
    mCR.openInputStream(imageUri),  null,  options);

OutputStream os = new FileOutputStream(f);
mBitMap.compress(Bitmap.CompressFormat.JPG, minVal, os);
mload.memoryCache.putbig(file.toURI().toURL().toString(), mBitMap);
您可以通过将位图的ByteArray加载到实体中,直接以http方式发布位图

case POST:

HttpPost httpPost = new HttpPost(url);                  
ByteArrayOutputStream stream = new ByteArrayOutputStream();                 
float tmp = (float) 1024 * 1024 / bmp.getByteCount();
int minVal = (Math.round(tmp * 100) < 101) ? Math.round(tmp * 100): 100;
if (bmp.compress(Bitmap.CompressFormat.JPEG, minVal, stream)){      
    httpPost.setEntity(new ByteArrayEntity(stream.toByteArray()));
}else{ //TODO need to format actual message
    handler.sendMessage(Message.obtain(handler,
    HttpConnection.DID_ERROR, new Exception("ERR bitmap NG")));
                    }
案例帖子:
HttpPost HttpPost=新的HttpPost(url);
ByteArrayOutputStream=新建ByteArrayOutputStream();
float tmp=(float)1024*1024/bmp.getByteCount();
int minVal=(数学四舍五入(tmp*100)<101)?数学四舍五入(tmp*100):100;
如果(bmp.compress(Bitmap.CompressFormat.JPEG,minVal,stream)){
setEntity(新的ByteArrayEntity(stream.toByteArray());
}否则{//TODO需要格式化实际消息
handler.sendMessage(Message.get(handler,
HttpConnection.DID_错误,新异常(“ERR bitmap NG”);
}
POST方法的背景是

是一个很好的模板

那么,既然可以直接对位图中的字节进行操作,为什么还要对文件使用mimultipartentity呢?一旦有了Uri,就获取一个位图,并使用位图/Uri对作为memCache接口、HTTP POST接口、FileLink接口的基础,当您有CacheMiss时,这些接口用于检索本地文件。这将有助于最大限度地减少在文件的基础上做任何事情

考虑使用Map[hashON(Uri.toString()::bitMap]来存储本地处理的图像。然后,当您想要发布图像时,您可以从映射中检索图像,并将其字节直接发布到“ByteArrayEntity”中

case POST:

HttpPost httpPost = new HttpPost(url);                  
ByteArrayOutputStream stream = new ByteArrayOutputStream();                 
float tmp = (float) 1024 * 1024 / bmp.getByteCount();
int minVal = (Math.round(tmp * 100) < 101) ? Math.round(tmp * 100): 100;
if (bmp.compress(Bitmap.CompressFormat.JPEG, minVal, stream)){      
    httpPost.setEntity(new ByteArrayEntity(stream.toByteArray()));
}else{ //TODO need to format actual message
    handler.sendMessage(Message.obtain(handler,
    HttpConnection.DID_ERROR, new Exception("ERR bitmap NG")));
                    }