繁体   English   中英

如何从下载的临时文件中获取字节数组?

[英]How do i get byte array from downloaded temp file?

我正在尝试从临时文件中获取字节数组。 我知道我的连接有效,因为我获得了地图字符串的正确值。 但是我一直得到一个空字节数组。 请帮忙! 任何帮助是极大的赞赏!

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

我更换了

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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