2016-03-11 135 views
2

我正在嘗試使用Picasso將圖像加載到ImageView。最基本的東西。畢加索將無法加載圖像(Android)

我知道這很簡單,我很確定我做得很好,但是由於某種原因它不起作用。沒有照片顯示在我的應用程序。我發現有人遇到同樣的問題(例如,Android - Picasso in Android Studio doesn't load),我的代碼與他們的固定版本幾乎完全相同,但它仍然無效。

這是我的代碼。

在MainActivity.java,我有

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if(savedInstanceState == null) { 
     getFragmentManager().beginTransaction() 
       .add(R.id.container, new MyFragment()) 
       .commit(); 
    } 

而在MyFragment.java,我有

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    ImageView image = (ImageView) rootView.findViewById(R.id.movie_image); 
    Picasso.with(getActivity()).load(Uri.parse("http://i.imgur.com/DvpvklR.png")).into(image); 

    return rootView; 
} 

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/movie_image" /> 

</FrameLayout> 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.admin.popmovies"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

    <user-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

</manifest> 

的build.gradle(模塊:APP)

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.example.admin.popmovies" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
} 

repositories { 
    mavenCentral(); 
} 

任何建議,將不勝感激!

編輯:logcat的 我得到了一些消息在logcat中畢加索以前,但我檢查了一遍,現在還沒有從畢加索日誌。 它看起來酷似有看起來像這樣同樣的問題,其他線程..

02-23 16:41:36.857 28865-28865/com.example.enrico.prova D/Picasso﹕ Main  created  [R1] Request{http://www.online-image-editor.com//styles/2014/images/example_image.png} 
02-23 16:41:36.858 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher enqueued  [R1]+0ms 
02-23 16:41:36.876 28865-29238/com.example.enrico.prova D/Picasso﹕ Hunter  executing [R1]+16ms 
02-23 16:41:36.883 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher batched  [R1]+25ms for error 
02-23 16:41:37.112 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher delivered [R1]+254ms 
02-23 16:41:37.112 28865-28865/com.example.enrico.prova D/Picasso﹕ Main  errored  [R1]+255ms 

UPDATE:
我想通了!這是因爲我的應用中的目標SDK版本是23,而我的手機使用的是KitKat。當我將gradle中的依賴項改爲compile 'com.android.support:appcompat-v7:21.0.2'而不是compile 'com.android.support:appcompat-v7:23.1.1'時,它起作用。

再次感謝您的所有建議!

+0

load(「http://i.imgur.com/DvpvklR.png」)使用此 – Madhur

回答

1

嘗試

Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image); 
+0

不幸的是,這對我不起作用...代碼中是否還有其他可能的缺陷? – Mind

+0

你有沒有碰到日誌? – thuongle

+1

您的清單中是否有互聯網許可? –

1

我認爲你不需要解析URI。嘗試不Uri.parse

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    ImageView image = (ImageView) rootView.findViewById(R.id.movie_image); 
    Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image); 

    return rootView; 
} 
1

這個工作對我來說:

Picasso.with(getActivity()) 
    .load("http://i.imgur.com/DvpvklR.png") 
    .placeholder(R.drawable.placeholder) 
    .error(R.drawable.error) 
    .resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) 
    .centerInside() 
    .into(image); 
+0

感謝您的回覆!我嘗試過,但仍然沒有照片顯示在我的應用程序。代碼中是否還有其他可能的缺陷? – Mind

+0

我修改了帖子,即使它是相同的 試試吧!如果你有任何崩潰的日誌讓我知道你沒有任何損失 ? – MrZ

+0

我試過了,我得到了錯誤照片。我在上面的帖子末尾添加了日誌。你能看看嗎?非常感謝! – Mind

0

它的工作對我來說用下面的代碼:

Picasso.with(this) 
       .load("http://i.imgur.com/DvpvklR.png") 
       .into(myImageView); 
0

我用畢加索不止一次。 FYI:(pic_link是JsonElement) 希望本文能夠幫助您 這是我建立我的方法:

public void updatePic(JsonElement jsonObject){ 
    Activity.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      final ImageView imageUser=(ImageView)findViewById(R.id.user_profile_photo); 
      final Target target = new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        imageUser.setImageBitmap(bitmap); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 

       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 

       } 
      }; 
      imageUser.setTag(target); 

      Picasso.with(getApplicationContext()) 
        .load(pic_link.getAsString()) 
        .into(target); 
     } 
    }); 
} 
0

主要錯誤是在你的清單文件。 你應該改正<user-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.INTERNET" />和所有圖片的工作!