我正在尝试将从我的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;
}
}
好像找不到我的档案?你认为什么是错的?
答案 0 :(得分:1)
我建议通过在File source = new File(...)行之后添加几行来检查是否存在(存在),然后尝试创建该文件(createNewFile)。有关详细信息,请参阅File API docs。然后,您可以查看新文件的创建位置,以便了解文件系统的实际位置。
此外,虽然您提供的FileInputStream代码有效,但在存储库中使用内置的uploadFromFile方法可能更容易。