使用 asp.net WebService 和 Android 将图像上传到 Azure Blob 存储?

2024-01-03

我正在尝试通过以下方式将选定的图像从我的 Android 设备上传到 Azure Blob

我制作的 asp.net WebService。

但我在 android 中收到橙色错误:“W/System.err(454): SoapFault - 故障代码:'soap:Server' 故障字符串:'服务器无法处理请求。 ---> 对象引用未设置为实例一个东西。'故障因素:“空”详细信息:org.kxml2.kdom.Node@4205f358 ”

我不确定是我的 Java 代码还是 Web 服务有问题......

这是两个代码:

网络服务:

    [WebMethod]
    public string UploadFile(string myBase64String, string fileName)
    {
        byte[] f = Convert.FromBase64String(myBase64String);

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve a reference to a container. 
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        container.SetPermissions(
         new BlobContainerPermissions
         {
             PublicAccess = BlobContainerPublicAccessType.Blob
         });

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

        using (MemoryStream stream = new MemoryStream(f))
        {
            blockBlob.UploadFromStream(stream);
        }

        return "OK";
    }

我已经在 Forms .net 中测试了这段代码,在解析 Base64 字符串并将其转换为 byte[] 时它工作得很好。 所以我不认为 WebService 代码是错误的..

请帮我!

这是Java->Android:

private String TAG = "PGGURU";
Uri currImageURI;
    String encodedImage;

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

    // To open up a gallery browser
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}

byte[] b;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

        if (resultCode == RESULT_OK) {

                if (requestCode == 1) {
                        // currImageURI is the global variable I'm using to hold the content:// URI of the image
                        currImageURI = data.getData();
                        String ImageUri = getRealPathFromURI(currImageURI);

                        Bitmap bm = BitmapFactory.decodeFile(ImageUri);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                        b = baos.toByteArray(); 
                            //encoded image to Base64
                        encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

                      //Create instance for AsyncCallWS
                        AsyncCallWS task = new AsyncCallWS();
                        task.execute();
                }
        }
}

public void UploadImage(String image, String imageName) {
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //Property which holds input parameters
    PropertyInfo PI = new PropertyInfo();
    PI.setName("myBase64String");
    PI.setValue(image);
    PI.setType(String.class);
    request.addProperty(PI);

    PI=new PropertyInfo();
    PI.setName("fileName");
    PI.setValue(imageName);
    PI.setType(String.class);
    request.addProperty(PI);

    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        //Invole web service
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        //Assign it to fahren static variable


    } catch (Exception e) {
        e.printStackTrace();
    }
}


private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        UploadImage(encodedImage, "randomName");
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");

    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}

PS:我已授予 Internet、WRITE_EXTERNAL_STORAGE 和 RECORD_AUDIO 使用权限


最后我解决了这个问题:D wihu!

在 WebService 中我必须更改:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.GetSetting("StorageConnectionString"));

对此(几乎相同):

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

然后转到 VS12 中的=>“管理 nuget 包”,并安装 Windows Azure 存储。

此外,我必须移动变量: byte[] f = Convert.FromBase64String(myBase64String);

在方法之外,如下所示:

    byte[] f;
    [WebMethod]
    public string UploadFile(string myBase64String, string fileName)
    {
         f = Convert.FromBase64String(myBase64String);
    }

就是这样。

所以 WebService 看起来像这样:

byte[] f;
    [WebMethod]
    public string UploadFile(string myBase64String, string fileName)
    {
        f = Convert.FromBase64String(myBase64String);


        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve a reference to a container. 
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        container.SetPermissions(
         new BlobContainerPermissions
         {
             PublicAccess = BlobContainerPublicAccessType.Blob
         });

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

        using (MemoryStream stream = new MemoryStream(f))
        {
            blockBlob.UploadFromStream(stream);
        }
        return "OK";
    }

这会将图像作为 ByteArray 发送到 Windows Azure 存储。

下一步是下载文件并将其转换为位图图像:)

如果这有帮助,请给我一些积分:D

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 asp.net WebService 和 Android 将图像上传到 Azure Blob 存储? 的相关文章

随机推荐

  • iOS 8.3 打破了自动单元格高度

    长期读者 第一次海报 我在我的应用程序中使用自动单元格高度和自动布局 在 iOS 8 3 和 8 4 中 这一点似乎被打破了 我有一个示例项目 当内置于 8 2 或更低版本时 它可以正常工作 单元格高度由自动布局确定 当内置于 8 3 或
  • PHP gettext() 挪威语

    我正在使用 PHPgettext 简单地将网站转换为其他语言 到目前为止 该解决方案运行良好 英语 匈牙利语 因为我需要将挪威语翻译添加到新网站 当我设置挪威语言环境时setlocale LC ALL nb NO ISO8859 1 get
  • 更改条形图中条形的宽度 (R)

    我想知道如何更改 barchart 函数中条形的宽度 这是代码 rater1 lt c 0 75 0 66 0 73 0 63 barplot rater1 ylim c 0 1 axes TRUE names arg c A B C D
  • gcc给linux ELF添加了哪些功能?

    当用 c 或 asm 链接一个类似 helloworld 的程序时gcc它会将一些内容添加到结果可执行目标文件中 我只知道运行时动态链接器和 start但这些添加的功能是什么样的入口点呢 00000000004003f0 t deregis
  • 如何正确使用 axios params 和数组

    如何向查询字符串中的数组添加索引 我尝试像这样发送数据 axios get myController myAction params storeIds 1 2 3 我得到了这个网址 http localhost api myControll
  • JavaScript:获取数组中的平均对象?

    我试图想出一种方法来使代码变得简单 使用最少的循环和变量 但我遇到了麻烦 我想根据 值 获取数组 数字 中的平均对象 我觉得必须有一种数学方法来获得平均值 而无需在另一个循环中找到最接近的平均值 目前我有这个混乱 var numbers v
  • 无法在 Jenkins Pipeline 中显示 JUnit 测试结果

    我有一段 Jenkins 管道代码 我试图在我的角度代码上运行 JUnit 如果单元测试失败 Jenkins 必须停止管道 它正在工作 只是我看不到 最新测试结果 和 测试结果趋势 我正在使用 Jenkins 2 19 1 Jenkins
  • 导入 CSV 时选择指定行

    我有一个很大的 CSV 文件 我只想导入选择某些行 如果有 首先 我创建将导入的行的索引 然后我希望将这些行的名称传递给 sqldf 并返回指定行的完整记录 create the random rows ids that will be s
  • 安卓Mipmap?

    每当我尝试使用 AndroidStudio 生成新的 Android 项目时 它都会隐藏文件夹 drawables 我以前从未发生过这种情况 我环顾四周 发现它正在生成这个名为 mipmap 的文件夹 我搜索了一下 发现这与绘图类似 但这是
  • 尝试使用 woocommerce_new_order_item 挂钩保存订单项元数据

    Add meta to order item param int item id param array values return void function cart add meta data booking item id valu
  • 使用 Wikimedia API 获取位置

    如何使用 Mediawiki API 获取 Wikipedia 文章的城市 国家位置 假设我想确定圣家族大教堂位于哪个国家 哪个城市 我应该使用什么属性 尝试以下查询 And see 扩展 地理数据 https www mediawiki
  • React Router v4 中的嵌套路由

    我正在尝试设置一些嵌套路由来添加通用布局 检查一下代码
  • 在应用程序购买测试帐户无法在 IOS 中运行?

    我们正在使用沙盒测试帐户测试应用程序购买 在测试时它显示验证 在验证付款信息后 当我尝试在应用程序购买中测试时 它会将我重定向到应用程序商店 应用程序商店显示超时 我做错了什么吗 我还创建了另外三个沙箱测试帐户 用于在应用程序购买中进行测试
  • Parallel.ForEach 未生成所有线程

    我在使用 Parallel ForEach 时遇到一些问题 我需要模拟几个硬件组件 等待传入连接并回复它 我当前的代码如下 Task Factory StartNew gt components component gt var liste
  • 第 N 次和第 (N+1) 次出现之间的正则表达式字符串

    我试图找到两个特殊字符之间第 n 次出现的子字符串 例如 一 二 三 四 五 比如说 我正在寻找 第 n 次和第 n 1 次 第二次和第三次出现 之间的字符串字符 结果是 三 我想使用正则表达式来做到这一点 有人可以指导我吗 我当前的尝试如
  • 从 SQL Server 2008 调用非托管 C/C++ DLL 函数

    我有一个庞大的 C C 函数库 需要从 SQL Server 2008 调用 我编写了一个 C 适配器类 它从 Win32 DLL 加载这些函数DllImport并将它们暴露给 Net 代码 这在大多数 Net 应用程序中都可以正常工作 现
  • 如何将参数名称作为参数传递?

    我有这个代码 hobbies2 form cleaned data pop hobbies2 PersonneHobby objects filter personne obj delete for pk str in hobbies2 t
  • UpdateSourceTrigger=显式

    我正在创建一个包含多个文本框的 WPF 窗口 当用户按下 确定 按钮时 我希望所有文本框都被评估为非空白 我知道我必须将 TextBox 与 Explicit 的 UpdateSourceTrigger 一起使用 但我是否需要为每个文本框调
  • 在资源有限的环境中禁用背景视频

    对于基于网络的媒体播放器项目 我正在尝试使用简单的一些微妙的背景视频
  • 使用 asp.net WebService 和 Android 将图像上传到 Azure Blob 存储?

    我正在尝试通过以下方式将选定的图像从我的 Android 设备上传到 Azure Blob 我制作的 asp net WebService 但我在 android 中收到橙色错误 W System err 454 SoapFault 故障代