BitmapFactory.decodeFile 返回 null

发布于 2024-11-18 04:30:33 字数 4157 浏览 6 评论 0原文

我想从网络上的照片创建位图。 首先,我尝试使用 BitmapFactory.decodeStream 但由于错误而返回 null (http://code.google.com/p/android/issues/detail?id=6066)。 现在我正在保存图像,然后使用 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)

I want to create a Bitmap from a photo that is on the web.
First i tried to use BitmapFactory.decodeStream but it returned null because of a bug (http://code.google.com/p/android/issues/detail?id=6066).
now i am saving the image an then using BitmapFactory.decodeFile but it still returns null.

Any ideas whats wrong or for another workaround?

Thank you!

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-11-25 04:30:33

伙计,我还在我的应用程序中从互联网下载图像,它运行得很好。我用来下载的函数是:

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 线程继续可用。我使用了 AsyncTask (请参阅此处),以便(简化的)doInBackground 函数类似于:

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

我认为 this android 博客的 page 很好地解释了与此问题相关的内容。

Guy, I also download images from the Internet in my app and its working just fine. The function that I use to download is:

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;
}

I set another thread to download the image so the UI thread continues to be available. I used AsyncTask (see here) so that the (simplified) doInBackground function is something like:

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

I think this page at android's blog explains well something related to this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文