Azure Android:将照片上传到 BLOB 存储错误
Azure Android: Uploading a Photo to BLOB Storage error
我正在尝试将 selected 从我的 android 设备上传到我的 Azure Blob 存储。首先我 select 我的图库中的一张图片,然后它将图片上传到我的云存储。但它抛出这个错误:
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;
}
}
好像找不到我的文件?大家觉得哪里不对?
我建议通过在 File source = new File(...) 行之后添加几行来检查是否存在 (exists),然后尝试创建该文件 (createNewFile) 进行调试。有关详细信息,请参阅 File API docs。然后您可以看到新文件的创建位置,这将使您了解文件系统的实际位置。
此外,虽然您拥有的提供 FileInputStream 的代码是有效的,但使用存储库中的内置 uploadFromFile 方法可能更容易。
我正在尝试将 selected 从我的 android 设备上传到我的 Azure Blob 存储。首先我 select 我的图库中的一张图片,然后它将图片上传到我的云存储。但它抛出这个错误:
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;
}
}
好像找不到我的文件?大家觉得哪里不对?
我建议通过在 File source = new File(...) 行之后添加几行来检查是否存在 (exists),然后尝试创建该文件 (createNewFile) 进行调试。有关详细信息,请参阅 File API docs。然后您可以看到新文件的创建位置,这将使您了解文件系统的实际位置。
此外,虽然您拥有的提供 FileInputStream 的代码是有效的,但使用存储库中的内置 uploadFromFile 方法可能更容易。