0

I am using Titanium to build a cross-platform mobile application, which I mostly tested with the iOS simulator and where I already got all functionality working. Now I want to have the app also bugfree on Android. One of the issues I'm facing right now is that the remote images are not shown anymore (while they were on iOS). The url from the remote images can is retrieved from the server and should be correct, as I see the images on iOS. This is one of the image urls: http://elgg.masaer.com/mod/profile/icondirect.php?joindate=1426024336&guid=47&size=large This is the code I use to show the image:

var profilePic = Titanium.UI.createImageView({
        image: post.User.avatar,
        width: '60px',
        height: '60px',
        borderRadius: 5
    });

Does anybody know what could be the issue. Maybe it is because the url doesn't really have an extension of the image file? Thanks in advance!

JasperTack
  • 4,337
  • 5
  • 27
  • 39

2 Answers2

0

possible answer is this

import java.net.URLConnection;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView mImgView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    mImgView1 = (ImageView) findViewById(R.id.mImgView1);
    String url = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    Bitmap bm = loadBitmap(url, bmOptions);
    mImgView1.setImageBitmap(bm);
}

public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (IOException e1) {
    }
    return bitmap;
}

private static InputStream OpenHttpConnection(String strURL)
        throws IOException {
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    } catch (Exception ex) {
    }
    return inputStream;
}
}
Community
  • 1
  • 1
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
  • 1
    Thank you for the help, but I am actually developing cross-platform mobile applications with Titanium. I will update my question to be more clear. Thanks anyway. – JasperTack Mar 14 '15 at 16:36
0

After some searching I found out that android is not smart enough to work with image urls and you have to retrieve the imagedata yourself. This can be used to download the remote image.

function loadImage(imageView,url) {
   var http = Titanium.Network.createHTTPClient();

    http.onload = function() {
     imageView.image=this.responseData;
    };

    http.open('GET',url);

    http.send();
};
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
JasperTack
  • 4,337
  • 5
  • 27
  • 39
  • 1
    It's because your URL use HTTP redirect, it's a know bug on Android : https://jira.appcelerator.org/browse/TIMOB-12885. So yes, the trick is to create a HTTP request for now. You can try this module to cache your image for avoid to call a request each time : http://gitt.io/component/To.ImageCache – Thomas Lemaitre Mar 14 '16 at 17:57