【问题标题】:Titanium android remote image not shownTitanium android远程图像未显示
【发布时间】:2015-03-14 16:08:31
【问题描述】:

我正在使用 Titanium 构建一个跨平台的移动应用程序,我主要使用 iOS 模拟器对其进行测试,并且我已经让所有功能正常工作。现在我想让该应用程序在 Android 上也没有错误。我现在面临的问题之一是不再显示远程图像(当它们在 iOS 上时)。远程图像的 url 可以从服务器检索,并且应该是正确的,因为我在 iOS 上看到了图像。这是图片网址之一: http://elgg.masaer.com/mod/profile/icondirect.php?joindate=1426024336&guid=47&size=large 这是我用来显示图像的代码:

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

有谁知道可能是什么问题。也许是因为网址实际上没有图像文件的扩展名?提前致谢!

【问题讨论】:

    标签: android image imageview titanium


    【解决方案1】:

    可能的答案是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;
    }
    }
    

    【讨论】:

    • 感谢您的帮助,但我实际上正在使用 Titanium 开发跨平台移动应用程序。我将更新我的问题以更清楚。还是谢谢。
    【解决方案2】:

    经过一番搜索,我发现 android 不够聪明,无法处理图像 url,您必须自己检索图像数据。这可用于下载远程图像。

    function loadImage(imageView,url) {
       var http = Titanium.Network.createHTTPClient();
    
        http.onload = function() {
         imageView.image=this.responseData;
        };
    
        http.open('GET',url);
    
        http.send();
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多