我正在尝试从临时文件中获取字节数组。我知道我的连接有效,因为我得到了地图字符串的正确值。但我一直得到一个空字节数组。请帮忙!非常感谢任何帮助!
package packagename
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlob;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.ListBlobItem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Map;
public class Getfilereference extends AsyncTask<Map<String,byte[]>,Void,Map<String,byte[]>> {
public Context mContext;
public Getfilereference(Context context) {
mContext = context;
}
@Override
protected Map<String, byte[]> doInBackground(Map<String, byte[]>... params) {
Map<String, byte[]> dictionary = new Hashtable<>();
try {
final String storageConnectionString =
"myconnectionstring";
final String azureblobstoragecontainername = "mycontainer";
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(azureblobstoragecontainername);
for (ListBlobItem blobItem : container.listBlobs()) {
if (blobItem instanceof CloudBlob) {
File file;
file = File.createTempFile("familyimages", null, mContext.getCacheDir());
CloudBlob blob = (CloudBlob) blobItem;
blob.download(new FileOutputStream(file + "\\" + blob.getName()));
FileInputStream fis = new FileInputStream(file + "\\" + blob.getName());
byte[] t = new byte[(file + "\\" + blob.getName()).length()];
fis.read(t);
fis.close();
dictionary.put(blob.getName(), t);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dictionary;
}
@Override
protected void onPostExecute(Map<String, byte[]> dictionary2) {
DirectoryOpenHelper dbhelper = new DirectoryOpenHelper(mContext);
for (Map.Entry<String, byte[]> entry : dictionary2.entrySet()) {
String key = entry.getKey();
byte[] value = entry.getValue();
dbhelper.openDB();
dbhelper.insertfamilyimageinrow(value, Integer.valueOf(key));
Log.i("Info",key);
}
}
}
答案 0 :(得分:0)
我替换了
byte[] t = new byte[(file + "\\" + blob.getName()).length()];
fis.read(t); fis.close(); dictionary.put(blob.getName(), t);
与
RandomAccessFile f = new RandomAccessFile(file+"\\"+blob.getName(), "r");
byte[] b = new byte[(int)f.length()]; f.read(b);
dictionary.put(blob.getName(), b);