2011-03-17 4 views
1

J'utilise ce code cette fois-ci. Je me demande ce qui ne va pas cette fois-ci? J'utilise un code qui charge l'image du net. Comme vous pouvez le voir. Mais Image perd sa qualité en décodant Bitmap (peut-être je me trompe). Vous pouvez consulter cette image. enter image description hereEst-ce un bug pour BitmapFactory lors du décodage d'InputStream?

J'ai essayé

  1. Obtenir l'image de l'URL et enregistrer que l'image .jpg dans drawable. puis charger l'image à partir des ressources. Cela fonctionne parfaitement cette fois. MAIS
  2. Lorsque j'obtiens Bitmap de l'URL au lieu des ressources, alors il faut faire plus de BLUR, vous pouvez le vérifier d'en haut.

Son look comme il peut y avoir problème ba dans BitmapFectory de de

public class MurgeActivity extends Activity { 
String abcc = "http://c3.ac-images.myspacecdn.com/images02/84/s_48e1fd15504c49ed8fd008f8592f3082.jpg"; 
String abcd = "http://c1.ac-images.myspacecdn.com/images02/92/s_3bf09d34200c4423a2b8cb73f12675b8.jpg"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    try { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView img1 = (ImageView) findViewById(R.id.cus_image); 
     ImageView img4 = (ImageView) findViewById(R.id.cus_image4); 

     // Here I am getting image from net. 
     Bitmap map = getImageFromWeb("http://c3.ac-images.myspacecdn.com/images02/84/s_48e1fd15504c49ed8fd008f8592f3082.jpg"); 

     // Here I am getting image from resources 
     //Bitmap map = BitmapFactory.decodeResource(getResources(),R.drawable.a1); 

     Bitmap enlarge = BitmapFactory.decodeResource(getResources(), R.drawable.enlarge_image_button); 
     img1.setImageBitmap(map); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     map.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] b = baos.toByteArray(); 

     String encodedImage = Base64.encodeBytes(b); 
     String items = combineArticleImage(encodedImage, enlarge); 
     String imgTag = "<img width='100' src='data:image/png;base64," + items 
       + "' align='left' style='padding-right:5px; padding-bottom:5px'/>"; 

     WebView webView = (WebView) findViewById(R.id.web); 
     webView.setBackgroundColor(R.color.black); 
     webView.loadData(imgTag, "text/html", "utf-8"); 

     byte[] decodedString = Base64.decode(items, android.util.Base64.DEFAULT); 
     Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
     Drawable end = new BitmapDrawable(decodedByte); 

     Drawable drb = Utils.combineSlidshowImages(new BitmapDrawable(map).getCurrent(), enlarge); 
     img4.setImageDrawable(drb); 
    } catch (Exception e) { 
     Log.e("Exeption", this.getClass().getSimpleName() + e.toString()); 
    } catch (Error error) { 
     Log.e("Error", this.getClass().getSimpleName() + error.toString()); 
    } 

} 

public static String combineArticleImage(String baseImage, Bitmap frontImage) { 

    String base64Image = null; 
    byte[] imagesBytes = null; 
    try { 
     imagesBytes = Base64.decode(baseImage); 

     if (imagesBytes != null) { 


      Bitmap map = BitmapFactory.decodeByteArray(imagesBytes, 0, imagesBytes.length); 

      Bitmap icon = frontImage; 
      Bitmap finalImag = combineImages(map, icon); 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      finalImag.compress(Bitmap.CompressFormat.JPEG, 100, baos); 

      byte[] b = baos.toByteArray(); 
      base64Image = Base64.encodeBytes(b); 
     } 
    } catch (Exception e) { 
     Log.e("Error", "Combining Article Image"); 
    } 
    return base64Image; 
} 

public static Bitmap getImageFromWeb(String imglink) { 
    Bitmap bmpImage = null; 
    try { 

     URL imgURL = new URL(imglink); 
     URLConnection conn = imgURL.openConnection(); 
     conn.connect(); 

     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 

     bmpImage = BitmapFactory.decodeStream(bis); 
     bis.close(); 
     is.close(); 

    } catch (Exception e) { 
     Log.e("Exeption", e.toString()); 
    } catch (Error error) { 
     Log.e("Error", error.toString()); 
    } 
    return bmpImage; 
} 

public static Bitmap combineImages(Bitmap backgroungImage, Bitmap frontImage) { // can 
    Bitmap cs = null; 
    try { 

     cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), backgroungImage 
       .getConfig()); 

     Canvas comboImage = new Canvas(cs); 
     comboImage.drawBitmap(backgroungImage, 0, 0, null); 
     comboImage.drawBitmap(frontImage, backgroungImage.getWidth() - frontImage.getWidth(), backgroungImage 
       .getHeight() 
       - frontImage.getHeight(), null); 

    } catch (Exception e) { 
     Log.e("Error", "Exception = " + e.toString()); 
    } catch (Error e) { 
     Log.e("Error", "Message = " + e.toString()); 
    } 
    return cs; 
} 

} main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:background="@color/black" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:paddingRight="2dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="4dp" 
    android:orientation="horizontal" 
    android:layout_marginRight="4dp"> 
    <ImageView 
     android:id="@+id/cus_image" 
     android:src="@drawable/preview_unavailable" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:background="@color/transparent" 
     android:scaleType="fitCenter" /> 
    <ImageView 
     android:id="@+id/cus_image4" 
     android:src="@drawable/preview_unavailable" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:background="@color/black" 
     android:scaleType="fitCenter" /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:paddingRight="2dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="4dp" 
    android:orientation="horizontal" 
    android:layout_marginRight="4dp"> 
    <WebView 
     android:id="@+id/web" 
     android:layout_width="wrap_content" 
     android:background="@color/black" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

Répondre

1

Avez-vous essayé

Options options = new BitmapFactory.Options(); 
options.inScaled = false; 
Bitmap source = BitmapFactory.decodeResource(activityName.getResources(), R.id.image, options); 

Au lieu de:

cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), backgroungImage.getConfig()); 

essayer:

+0

Merci pour votre temps. Mais il y a toujours un problème ... – Arslan

+0

Où je me trompe? Ce n'est pas une façon correcte? – Arslan

+0

Vérifiez à nouveau ma réponse Je l'ai édité. – Lumis

0

Lorsque l'image est dans les ressources, puis Android l'adapter avec la bonne densité de pixels. Quand il vient du Web il ne connaît pas la densité et essaye avec la densité actuelle de téléphone.

Essayez de définir une densité fixe avant le décodage:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inDensity = android.util.DisplayMetrics.DENSITY_MEDIUM; 
Bitmap map = BitmapFactory.decodeByteArray(imagesBytes, 0, imagesBytes.length); 

bazinga

+0

Merci pour votre temps. Mais ça me donne toujours le même résultat? – Arslan

Questions connexes