3 Replies - 407 Views - Last Post: 24 April 2014 - 05:30 PM

#1 Anonymous1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

How to load an Image in Android

Posted 23 April 2014 - 07:10 PM

I have been trying to draw a bitmap image onto the canvas from Android and all it displays is a blank white screen. Whenever my app starts up, I am calling this in my game - view class which extends View and overrides the onDraw method:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Player {
    private int x, y;
    private Bitmap bmp;
    private FileInputStream s;
    File file;

    public Player(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public void render(Canvas c) throws FileNotFoundException, IOException {
        file = new File("C:\\Users\\Anonymous\\Desktop");
        s = new FileInputStream(file);
        bmp = BitmapFactory.decodeStream(s);
        Paint p = new Paint();
        c.drawBitmap(bmp,0,0,p);
        s.close();
    }

    public void tick() {
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to load an Image in Android

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10700
  • View blog
  • Posts: 39,813
  • Joined: 27-December 08

Re: How to load an Image in Android

Posted 23 April 2014 - 08:04 PM

Just a heads up that we have an Android forum. I'll move your thread there. :)
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,048
  • Joined: 15-July 08

Re: How to load an Image in Android

Posted 24 April 2014 - 09:40 AM

This is android right? If so, you definitely do not have a file called "C:\\Users\\Anonymous\\Desktop" on your system....Android is Unix based. You should instead embed your image as an Asset.

http://developer.and...setManager.html
Was This Post Helpful? 0
  • +
  • -

#4 Anonymous1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: How to load an Image in Android

Posted 24 April 2014 - 05:30 PM

View PostDogstopper, on 24 April 2014 - 09:40 AM, said:

This is android right? If so, you definitely do not have a file called "C:\\Users\\Anonymous\\Desktop" on your system....Android is Unix based. You should instead embed your image as an Asset.

http://developer.and...setManager.html

I have tried that so its not working what I tried right now is this in my player class:
    public void render(Canvas c) throws FileNotFoundException, IOException
    {
        Drawable d = activity.getResources().getDrawable(R.drawable.galaga);
        d.draw(c);




    }


which is called in my GameView Class which extends View and uses the onDraw Method here :
package com.yeshua.game;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;

import java.io.FileNotFoundException;
import java.io.IOException;


/**
 * Created by Anonymous on 4/22/14.
 */
public class GameView extends View implements Runnable{
    private Bitmap bitmap;
    private Thread thread;
    private boolean running = false;
    private Player p;
    private MainActivity activity;
    public GameView(Context context,MainActivity activity) throws FileNotFoundException
    {
        super(context);

        bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        p = new Player(2,3,activity);
    }

    public synchronized void start()
    {
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }
    public synchronized void stop()
    {
        if(!running)
            return;

        running = false;
        System.exit(0);

    }

    @Override
    public void run()
    {
           Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
           Canvas c = new Canvas(B)/>;
            while(running)
            {
                onDraw(c);
                tick();

            }

    }

    public void tick()
    {


    }


    @Override
    protected void onDraw(Canvas canvas)
    {
        try{

            p.render(canvas);


        }catch(FileNotFoundException e )
        {

        }
        catch (IOException e)
        {

        }


    }

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1