Images (base64) do not load correctly

Unfortunately, I am encountering some problems when trying to upload some images from an Android device to the database. Images are in the folder. This folder contains images as well as other materials. I do not know the names of the images, and I need to upload only the images (jpg). Before uploading the images, I need to encode them using base64.

First I get the jpg files from the folder. Then I get the ID from the image name. After that, I encode it through base64:

Button upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        String path = Environment.getExternalStorageDirectory().getPath();
        File dir = new File(path);
        File[] files = dir.listFiles();

        for (int i = 0; i < files.length; ++i) {
            if (files[i].getName().endsWith(".jpg")) {
                pics = new File(String.valueOf(files[i]));
                id = String.valueOf(files[i]);
                String sub = id.substring(id.lastIndexOf("/") + 1);
                int index = sub.indexOf("_");
                String book;
                if (index >= 0) {
                    book = sub.substring(0, index);
                    ID = book;
                    Log.e("ID", ID);
                }
                Bitmap imagex = BitmapFactory.decodeFile(pics.getAbsolutePath());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                imagex.compress(Bitmap.CompressFormat.JPEG, 70, baos);
                byte[] b = baos.toByteArray();
                Image = Base64.encodeToString(b, Base64.DEFAULT);

                try {
                    new HttpAsyncTask(ID,Image,Nummer).execute("https://....");
                } catch (Exception e) {
                    Log.e("InputStream", e.getMessage());
                }

                Log.e("PICS", id);
            }
        }
    }
});

public String POST(String url) {
    InputStream inputStream;
    try {

        HttpClient httpclient = classxy.getNewHttpClient();

        HttpPost httpPost = new HttpPost(url);
        String json = "";

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("bookId", ID);
        jsonObject.put("imageString", Image);
        jsonObject.put("imageNumber", Nummer);

        json = jsonObject.toString();

        StringEntity se = new StringEntity(json);

        httpPost.setEntity(se);

        httpPost.setHeader("Apikey", data);
        httpPost.setHeader("Modul", "upload_image");

        HttpResponse httpResponse = httpclient.execute(httpPost);
        inputStream = httpResponse.getEntity().getContent();


        if (inputStream != null)
            result = classxy.convertInputStreamToString(inputStream);
        else
            result = "Fehler!";
    } catch (Exception e) {
        Log.e("InputStream", e.getLocalizedMessage());
    }
    int num = Integer.parseInt(Nummer);
    num++;
    Nummer = Integer.toString(num);
    return result;
}

public class HttpAsyncTask extends AsyncTask<String, Void, String> {
    private final Object ID, Image, Nummer;

    public HttpAsyncTask(Object ID, Object Image, Object Nummer) {
        this.ID = ID;
        this.Image = Image;
        this.Nummer = Nummer;
    }

    protected String doInBackground(String... urls) {
        return POST(urls[0]);
    }

    protected void onPostExecute(String result) {
        if (result.matches("(.*)false(.*)")) {
            Toast.makeText(getApplicationContext(), "....", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show();
        }
        Log.e("RESPONSE", result);
    }
}

base64 . , . . , .

, ?

+4
2

, , .

ID, Image Nummer for. , , POST . :

, .

, . , , .

, . . Nummer , . int :

upload.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        String ID = "", Image;
        int Nummer = 0;
        [...]

        for (int i = 0; i < files.length; ++i) {
            if (files[i].getName().endsWith(".jpg")) {
                [...]

                try {
                    new HttpAsyncTask(ID,Image,Integer.toString(Nummer++)).execute("https://....");
                } catch (Exception e) {
                    Log.e("InputStream", e.getMessage());
                }

                Log.e("PICS", id);
            }
        }
    }
});

public String POST(String url, String ID, String Image, String Nummer) {
    InputStream inputStream;
    try {
        [...]
    } catch (Exception e) {
        Log.e("InputStream", e.getLocalizedMessage());
    }
    //int num = Integer.parseInt(Nummer);
    //num++;
    //Nummer = Integer.toString(num);
    return result;
}

public class HttpAsyncTask extends AsyncTask<String, Void, String> {
    private final String ID, Image, Nummer;

    public HttpAsyncTask(String ID, String Image, String Nummer) {
        this.ID = ID;
        this.Image = Image;
        this.Nummer = Nummer;
    }

    protected String doInBackground(String... urls) {
        return POST(urls[0], ID, Image, Nummer);
    }

    protected void onPostExecute(String result) {
        [...]
    }
}
+5

Asynctask for, , , .

, :

1) for loop , ArrayList

2) , ArrayList , ,

HttpAsyncTask (ID, Image, Integer.toString(Nummer ++)). execute ( "https://...." );

3) HttpAsyncTask onPostExecute(String result)

for loop (i=0;i<ArrayList.Size();i++) {
    ID=ArrayList first position data ID
    Image=ArrayList first position data IMAGE
    number=ArrayList first position data number

Call new HttpAsyncTask(ID,Image,Integer.toString(Nummer++)).execute("https://....");

}

, , , .

...

+1

All Articles