2017-06-22 8 views
0

だから私は、ユーザ名が画像の名前に対応するテーブルを持つデータベースを持っています。私のAsyncHTTPではUsernameを取得し、そのUsernameを正しいイメージにマッピングしたい(すべてのイメージは私のdrawableフォルダにあります)非同期で画像を更新する

私のXMLでは、このイメージがレンダリングされる場所にイメージビューを置き、画像(このimageViewのIDはmyImageです)

コードを実行すると、画像はレンダリングされません。スコープの問題のために、非同期以外のイメージを割り当てることはできません。

Context context = this; 



     AsyncHTTPPost asyncHttpPost2 = new AsyncHTTPPost(
       "http://lamp.ms.wits.ac.za/~s1363679/avatars.php", params) { 

      @Override 
      protected void onPostExecute(String s) { 


       try { 
        JSONArray all = new JSONArray(s); 
        for (int i = 0; i < all.length(); i++) { 

         JSONObject item = all.getJSONObject(i); 

         fName = item.getString("avatarName"); 
         System.out.println(); 
         Toast.makeText(Profile.this, fName, Toast.LENGTH_LONG).show(); 

         ImageView Image = (ImageView) findViewById(R.id.myImage); 
         System.out.println(""); 
         theName = fName; 
         //String imagename="kaius"; 

         Resources res = context.getResources(); 
         int resID = res.getIdentifier(theName, "drawable", context.getPackageName()); 
         Image.setImageResource(resID); 

        } 
       } catch (Exception e) { 
        Toast.makeText(Profile.this, e.toString(), Toast.LENGTH_LONG).show(); 
       } 
      } 
     }; 
     asyncHttpPost2.execute(); 



    } 

imageNameは正しく検索され、変数 "theName"に格納されますが、ビューには表示されません。あなたの描画可能なフォルダの同じレベルに

答えて

0

、また、これらの名前の一部のフォルダがあるはずです:

-drawable-hdpi

-drawable-xhdpi

-drawable-xxhdpi

-drawable-xxxhdpi

あなたの画像は異なるdpiに従って拡大縮小され、それらのフォルダ内に配置されて、すべての画面で正しく表示されます。あなたが代わりに行うことができ

もう一つは、次のとおりです。

// get drawable path 
    String imageUri = "drawable://" + R.drawable.image; 
    imageView.setImageBitmap(decodeSampledBitmapFromFile(imageUri , 1920, 1080)); 

そしてどこかdecodeSampledBitmapFromFileこのような宣言:

public static Bitmap decodeSampledBitmapFromFile(String imagePath, 
                int reqWidth, int reqHeight) { 
    // BitmapFactory.decodeFile(this.getFilesDir().getPath() + "/" + imagestoplay[currImage]) 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    // BitmapFactory.decodeResource(res, resId, options); 
    BitmapFactory.decodeFile(imagePath, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(imagePath, options); 

} 

ドントは、ピクセル単位で自分の必要なサイズに1080と1920を変更することを忘れ。

関連する問題