2011-06-30 16 views
0

ウェブ上にある写真からビットマップを作成したいと思います。 最初にBitmapFactory.decodeStreamを使用しようとしましたが、バグ(http://code.google.com/p/android/issues/detail?id=6066)のためnullを返しました。 今私はBitmapFactory.decodeFileを使用してイメージを保存していますが、それでもnullを返します。BitmapFactory.decodeFileがnullを返す

何か問題が何か間違っているか、別の回避策ですか?

ありがとうございました!

try { 
     URL uri = new URL("http://www.witzigundkraus.de/wp-content/uploads/2008/04/scooter-dj-munchen.jpg"); 
      URLConnection connection = uri.openConnection(); 
      Log.i(TAG, "connecting..."); 
      connection.connect(); 
      InputStream is = connection.getInputStream(); 
      //BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024); 

      File myfile = new File(getApplicationContext().getCacheDir(), "wallpaper.tmp"); 
      myfile.createNewFile(); 
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(myfile)); 

      byte buf[]=new byte[1024]; 
      int len; 
      while((len=is.read(buf))>0) 
      out.write(buf,0,len); 
      out.close(); 
      is.close(); 


      Log.d(TAG, String.valueOf(myfile.exists())); 
      Bitmap bmp = BitmapFactory.decodeFile(myfile.getName()); 
      //Bitmap bmp = BitmapFactory.decodeStream(bis); 


      Log.i(TAG, "setting bitmap"); 

      Matrix matrix = new Matrix(); 
      int scale = canvas.getWidth()/bmp.getWidth(); 
      matrix.postScale(scale, scale, bmp.getWidth(), bmp.getHeight()); 
      //matrix.postScale(0.5F, canvas.getWidth()/bmp.getWidth()); 

      Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, canvas.getWidth(), canvas.getHeight(), true); 

      Paint p = new Paint(); 
      p.setFilterBitmap(true); 


      try{ 
       canvas.drawBitmap(bmp2, matrix, p); 
      }catch(NullPointerException exc){ 
       //why do we get this?? 
       Log.d(TAG, "NullPointerException drawing canvas. why?"); 
       return; 
      } 


    } catch (MalformedURLException exc){ 
     Log.e(TAG, exc.toString()); 
     return; 
    } catch (IOException exc){ 
     Log.e(TAG, exc.toString()); 
     return; 
    } 










6-30 17:16:14.558 2253 2253 E AndroidRuntime: java.lang.NullPointerException 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.localfotos.MyWallpaperService$CubeEngine.drawCube(MyWallpaperService.java:212) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.localfotos.MyWallpaperService$CubeEngine.drawFrame(MyWallpaperService.java:159) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.localfotos.MyWallpaperService$CubeEngine.onSurfaceChanged(MyWallpaperService.java:109) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.service.wallpaper.WallpaperService$Engine.updateSurface(WallpaperService.java:543) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:591) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:787) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:45) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.os.Looper.loop(Looper.java:136) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4425) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:521) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
06-30 17:16:14.558 2253 2253 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 
+0

ああ、私は 'getName'の代わりに' getPath'を使う必要があります( - – nomoral

答えて

1

ガイ、私はまた、私のアプリでインターネットから画像をダウンロードして、うまく動作します。ダウンロードに使用する機能は次のとおりです。

public Bitmap download_Image(String url) { 
    Bitmap bm = null; 
    try { 
     URL aURL = new URL(url); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     bm = BitmapFactory.decodeStream(bis); 
     bis.close(); 
     is.close(); 
    } catch (IOException e) { 
     Log.e("Hub","Error getting the image from server : " + e.getMessage().toString()); 
    } 
    return bm; 
} 

UIスレッドが引き続き利用できるように画像をダウンロードする別のスレッドを設定しました。私はアンドロイドのブログでthis pageは、この問題に関連するだけでなく、何かを説明して考える

@Override 
protected Void doInBackground(String... urls) { 
     return download_Image(urls[0]); 
} 

:(簡体字)doInBackground機能のようなものであるように、私は(hereを参照)AsyncTaskを使用しました。