【发布时间】:2015-05-18 05:56:46
【问题描述】:
我正在尝试将我的 android 设备中的选定内容上传到我的 Azure Blob 存储。首先,我从我的图库中选择一张图片,然后将图片上传到我的云存储。但它会抛出这个错误:
java.io.FileNotFoundException: /content:/media/external/images/media/126310: open failed: ENOENT (No such file or directory)
这是我的代码:
Button choosePhoto = (Button) (findViewById(R.id.choosePhotoBtn));
choosePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.d("Testing", "Uploading");
// currImageURI is the global variable I'm using to hold the content:// URI of the image
currImageURI = data.getData();
TextView tv = (TextView) findViewById(R.id.photoPath);
tv.setText(currImageURI.toString());
new MyTask().execute();
}
}
}
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//Here starts the code for Azure Storage Blob
try{
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("photos");
// Create the container if it does not exist
container.createIfNotExists();
// Create a permissions object
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container
container.uploadPermissions(containerPermissions);
// Create or overwrite the "myimage.jpg" blob with contents from a local file
CloudBlockBlob blob = container.getBlockBlobReference("image_1055026155.jpg");
File source = new File(currImageURI.toString());
blob.upload(new FileInputStream(source.getAbsolutePath()), source.length());
Log.d("Testing", "Done");
}
catch(Exception e){
Log.e("SARATH", e.toString());
}
return null;
}
}
好像找不到我的文件?大家觉得哪里不对?
【问题讨论】:
-
根据错误消息,我认为您尝试上传的文件不存在,或者可能只是在您要查找的目录中不存在。
-
@EmilyGerner-Microsoft 它存在。我尝试了其他 10 张图片,他们也这样做了。您有可能使用过的代码示例吗?
-
不幸的是,我提供的一个样本只会显示我使用我的文件系统,而这对你的文件系统并没有真正的帮助。有关下一步尝试的一些想法,请参阅下面的答案。
标签: android azure azure-storage azure-mobile-services azure-blob-storage