我有一个应用程序,我试图从链接中检索图像,我通过谷歌集成。但是,每当我尝试将图片放入位图时,它总会给我一个错误。代码和错误如下:
代码:
if (google_user_gallery_pic.isEmpty() == true)
{
if (google_user_pic.isEmpty() == false)
{
onPost = null;
onPost2 = null;
onPost = getBitmapFromURL(google_user_pic);
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
else
{
onPost = null;
onPost2 = null;
onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
if (onPost == null)
{
onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
}
else
{
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
}
}
else
{
onPost = null;
onPost2 = null;
onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
if (onPost == null)
{
onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
//onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
else
{
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
}
错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: https:/lh3.googleusercontent.com/-HW5Tk9h1V2I/AAAAAAAAAAI/AAAAAAAADA8/7UbMeHbyFLM/photo.jpg: open failed: ENOENT (No such file or directory)
网址:
https://lh3.googleusercontent.com/-HW5Tk9h1V2I/AAAAAAAAAAI/AAAAAAAADA8/7UbMeHbyFLM/photo.jpg
此网址在一个页面上工作但在另一个页面上不起作用。这两个页面都是片段活动。任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
尝试将if和else条件反转为以下内容:
if (google_user_gallery_pic.isEmpty() == false)
{
if (google_user_pic.isEmpty() == false)
{
onPost = null;
onPost2 = null;
onPost = getBitmapFromURL(google_user_pic);
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
else
{
onPost = null;
onPost2 = null;
onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
if (onPost == null)
{
onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
}
else
{
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
}
}
else
{
onPost = null;
onPost2 = null;
onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
if (onPost == null)
{
onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
//onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
else
{
onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
}
}
因为给定了权限和其他任何内容而不是此代码段,除此之外没有任何其他错误
答案 1 :(得分:1)
尝试使用picasso从URL加载图像位图。
private Target mTarget; //declare variable
void loadImage(Context context, String url) {
final ImageView imageView = (ImageView) findViewById(R.id.image);
mTarget = new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
//Do something on the bitmap
imageView.setImageBitmap(bitmap); //set the bitmap
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(context)
.load(url)
.into(mTarget);
..error(R.drawable.the_smallperson) // will be displayed if the image cannot be loaded
}
在gradle中添加:
compile 'com.squareup.picasso:picasso:2.5.2'