简体   繁体   中英

How to upload image blobs to Azure from Android

I'm having a hard time uploading an image from an Android device to my Azure Cloud storage account.

Below is the code that i have gotten to work. However, the image being selected by the user is returning a URI, and i cannot get a solution working that involves converting the uri to a file path(what is hardcoded in the "working" example. Ive read online that filepaths are not acceptable anymore, so i have tried to convert the photo to bitmap and also tried using multiple solutions involving getContextResolver(). But everytime i try a different tactic, the file is not found or i get a null pointer exception.

//Code that works
final String filePath = "storage/emulated/0/DCIM/Camera/IMG_20190328_141613.jpg";
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
File source = new File(filePath);
blob.upload(new FileInputStream(source), source.length());
//Alternative 1 that doesnt work
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri,projection,null,null,null);
cursor.moveToFirst();
int colIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(colIndex);
cursor.close();
File source = new File(filePath);
blob.upload(new FileInputStream(source), source.length());

Any help with this issue is greatly appreciated.

After a good bit of research, i found what i think is the best solution so far. Basically i needed to copy the photo i was trying to send from external storage to internal by converting to a bitmap. Full solution below. Please let me know if anyone has a better one.

String connectionString = "{account key}";

CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectionString);

// 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({bucketName});

//Create new file
File f = new File(getApplicationContext().getCacheDir(), blobName);
f.createNewFile();

//Create byte array stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();

/*bitmap is loaded in the onCreate method (not shown in this block) Quality is 0-100 where 100 is the best*/
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);

byte[] bitmapdata = bos.toByteArray();

//write bitmap to file, flush and close.
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();

//upload bitmap from local directory
final String filePath = getApplicationContext().getCacheDir() + blobName;
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
File source = new File(f.getAbsolutePath());
blob.upload(new FileInputStream(source), source.length());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM