Android 从URL检索位图
虽然不是很优雅,但我使用了一种快速的方法来解决这个问题。然而,我仍然希望发现问题所在 关于flickr API 我有一个需要位图对象的图像,我可以访问用户idAndroid 从URL检索位图,android,bitmap,Android,Bitmap,虽然不是很优雅,但我使用了一种快速的方法来解决这个问题。然而,我仍然希望发现问题所在 关于flickr API 我有一个需要位图对象的图像,我可以访问用户idgrimneko和图片id8638235984 因此,我试过了 URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/"); Bitmap mp = BitmapFactory.decodeStream(url.openConnection(
grimneko
和图片id8638235984
因此,我试过了
URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
Bitmap mp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
但是,在这种情况下,mp是null
。我还尝试了www.google.com
作为url
,但这似乎也不起作用
然而,这确实有效
我的猜测是Bitmap
对象只能从具有图像扩展名的URL创建。这是正确的吗
如果是这种情况,如何在图像视图中呈现来自该flickr url的图像?试试这个
public static Bitmap getBitmapFromURL() {
try {
URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
也请参考此链接
私有类setProfileData扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
progressDialog=新建progressDialog(getActivity());
progressDialog.setMessage(“加载…”);
progressDialog.setCancelable(真);
progressDialog.show();
}
@凌驾
受保护位图doInBackground(无效…arg0){
试一试{
字符串imgurl=imageURL.replace(“,“%20”);
Log.e(“Profile pic path---------------->>>>>”,imgurl);
ProfilePicBitmap=null;
//URL=新URL(imgurl);
试一试{
位图配置文件picbitmap=null;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=8;
试一试{
ProfilePicBitmap=BitmapFactory.decodeStream(
(InputStream)新URL(imgurl.getContent(),null,options);
}捕获(例外e){
e、 printStackTrace();
试一试{
ProfilePicBitmap=BitmapFactory.decodeStream((InputStream)新URL(imgurl)
.getContent());
}捕获(异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
}捕获(例外e){
e、 printStackTrace();
ProfilePicBitmap=BitmapFactory.decodeResource(
getResources(),R.drawable.ic_启动器);
}
}捕获(例外e){
e、 printStackTrace();
}
返回位图;
}
@凌驾
受保护的void onPostExecute(位图结果){
super.onPostExecute(结果);
//关闭进度对话框
if(progressDialog.isShowing()){
progressDialog.disclose();
}
试一试{
if(ProfilePicBitmap!=null){
setImageBitmap(ProfilePicBitmap);
}否则{
setImageBitmap(位图工厂
.decodeResource(getResources(),
R.可牵引ic_发射器);
}
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
受保护的void onCancelled(){
super.onCancelled();
progressDialog.disclose();
}
}
Hi Tamilan,谢谢您的回复。但是,如果您查看上面的代码,它几乎是相同的。测试后,它仍然返回null。@user1431282您是否检查了此链接您是否获得了imageYes:)正如我在原始帖子中提到的,它确实可以处理该图像。我想知道为什么这种方法不适用于任何flickr图像。@user1431282,因为这不是单一图像,这就是为什么它不起作用的原因。@user1431282这里是相同的图像链接,请尝试使用此链接
private class setProfileData extends AsyncTask<Void, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading ..");
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected Bitmap doInBackground(Void... arg0) {
try {
String imgurl = imageURL.replace(" ", "%20");
Log.e("Profile pic path ----------->>>>>", imgurl);
ProfilePicBitmap = null;
// URL url = new URL(imgurl);
try {
Bitmap ProfilePicBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
ProfilePicBitmap = BitmapFactory.decodeStream(
(InputStream) new URL(imgurl).getContent(), null, options);
} catch (Exception e) {
e.printStackTrace();
try {
ProfilePicBitmap = BitmapFactory.decodeStream((InputStream) new URL(imgurl)
.getContent());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
ProfilePicBitmap = BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher);
}
} catch (Exception e) {
e.printStackTrace();
}
return ProfilePicBitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
try {
if (ProfilePicBitmap != null) {
ImageView.setImageBitmap(ProfilePicBitmap);
} else {
ImageView.setImageBitmap(BitmapFactory
.decodeResource(getResources(),
R.drawable.ic_launcher));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCancelled() {
super.onCancelled();
progressDialog.dismiss();
}
}