Reputation: 5084
I'm trying to upload a selected from from my android device to my Azure Blob Storage. First I select an image from my gallery and then it uploads the image to my Cloud Storage. But it throws this error:
java.io.FileNotFoundException: /content:/media/external/images/media/126310: open failed: ENOENT (No such file or directory)
Here's my code:
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;
}
}
It seems like it can't find my file? What do yall think is wrong?
Upvotes: 1
Views: 661
Reputation: 2457
I'd suggest debugging by adding a couple lines after the File source = new File(...) line that check for existence (exists) and maybe then try creating that file (createNewFile). See the File API docs for more info. You can then see where the new file is created which will give you an idea of where the file system is actually looking.
Also, while the code you have that gives a FileInputStream is valid, it may be easier to use the built-in uploadFromFile method in the storage library.
Upvotes: 1