0

I am trying to get byte array from temp file. I know my connection works because I am getting the correct values of the map's strings. But I keep getting a null byte array. Please help! Any help is greatly appreciated!

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);
        }
    }
}
juvchan
  • 6,113
  • 2
  • 22
  • 35
  • One common cause for this problem is that the pointer to read the stream is positioned at the end of the stream. Can you try by positioning the pointer to the start of the stream? – Gaurav Mantri Mar 18 '16 at 03:27
  • Exactly where should I put the reader? –  Mar 18 '16 at 03:30
  • 1
    Try checking position of `fis` after this line - `FileInputStream fis = new FileInputStream(file + "\\" + blob.getName())`; Alternately try something like mentioned here: http://stackoverflow.com/questions/858980/file-to-byte-in-java. – Gaurav Mantri Mar 18 '16 at 03:34
  • :) So what solved the problem? I'm curious to know. – Gaurav Mantri Mar 18 '16 at 03:51
  • When you say you're getting a null byte array, which expression exactly is returning null? – Doug Stevenson Mar 18 '16 at 05:19
  • I replaced byte[] t = new byte[(file + "\\" + blob.getName()).length()]; fis.read(t); fis.close(); dictionary.put(blob.getName(), t); """"""with"""""""" RandomAccessFile f = new RandomAccessFile(file+"\\"+blob.getName(), "r"); byte[] b = new byte[(int)f.length()]; f.read(b); dictionary.put(blob.getName(), b); –  Mar 18 '16 at 12:25

1 Answers1

0

I replaced

byte[] t = new byte[(file + "\\" + blob.getName()).length()]; 
fis.read(t); fis.close(); dictionary.put(blob.getName(), t); 

with

RandomAccessFile f = new RandomAccessFile(file+"\\"+blob.getName(), "r");
byte[] b = new byte[(int)f.length()]; f.read(b);
dictionary.put(blob.getName(), b);