簡體   English   中英

如何從Android上將圖像blob上傳到Azure

[英]How to upload image blobs to Azure from Android

我很難將圖像從Android設備上傳到我的Azure雲存儲帳戶。

下面是我已經開始工作的代碼。 但是,用戶選擇的圖像返回一個URI,我無法得到一個解決方案,它涉及將uri轉換為文件路徑(“工作”示例中的硬編碼。我在線閱讀文件路徑不再可接受了,所以我試圖將照片轉換為位圖,並嘗試使用涉及getContextResolver()的多個解決方案。但每次我嘗試不同的策略,找不到文件或我得到空指針異常。

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

非常感謝任何有關此問題的幫助。

經過一番研究,我發現到目前為止我認為是最好的解決方案。 基本上我需要通過轉換為位圖來復制我試圖從外部存儲器發送到內部的照片。 完整解決方案如下 如果有人有更好的,請告訴我。

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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM