Titanium android 远程图像未显示
Titanium android remote image not shown
我正在使用 Titanium 构建一个跨平台的移动应用程序,我主要使用 iOS 模拟器对其进行了测试,并且我已经在其中运行了所有功能。现在我想让应用程序在 Android 上也没有错误。我现在面临的问题之一是远程图像不再显示(当它们在 iOS 上时)。来自远程图像的 url 可以从服务器检索并且应该是正确的,正如我在 iOS 上看到的图像一样。这是其中一张图片 url:
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
});
有谁知道可能是什么问题。也许是因为 url 并没有图像文件的扩展名?提前致谢!
可能的答案是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;
}
}
经过一些搜索,我发现 android 不够智能,无法处理图像 url,您必须自己检索图像数据。这可以用来下载远程图像。
function loadImage(imageView,url) {
var http = Titanium.Network.createHTTPClient();
http.onload = function() {
imageView.image=this.responseData;
};
http.open('GET',url);
http.send();
};
我正在使用 Titanium 构建一个跨平台的移动应用程序,我主要使用 iOS 模拟器对其进行了测试,并且我已经在其中运行了所有功能。现在我想让应用程序在 Android 上也没有错误。我现在面临的问题之一是远程图像不再显示(当它们在 iOS 上时)。来自远程图像的 url 可以从服务器检索并且应该是正确的,正如我在 iOS 上看到的图像一样。这是其中一张图片 url: 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
});
有谁知道可能是什么问题。也许是因为 url 并没有图像文件的扩展名?提前致谢!
可能的答案是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;
}
}
经过一些搜索,我发现 android 不够智能,无法处理图像 url,您必须自己检索图像数据。这可以用来下载远程图像。
function loadImage(imageView,url) {
var http = Titanium.Network.createHTTPClient();
http.onload = function() {
imageView.image=this.responseData;
};
http.open('GET',url);
http.send();
};