0

I am trying to take a Bitmap and display it in an ImageView.

ImageView iv = new ImageView(this);

Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
iv.setImageBitmap(bMap);

That's my code for it. I create an ImageView and a Bitmap. I want to display my Bitmap in my ImageView. But I always get these two errors on the iv.setImageBitmap(bMap); statment

Syntax error on token "bMap", VariableDeclaratorId expected after this token    
Syntax error on token(s), misplaced construct(s)    

Does anybody has an idea why this happens and what i have to change?

Tim
  • 1
  • 3
  • Maybe [this](http://stackoverflow.com/questions/19716378/string-declaration-variabledeclaratorid-expected-after-this-token) link help you and this will be a true answer. – GIGAMOLE Feb 01 '15 at 12:44
  • Why you using decodeFile? You should use `Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.example);` if you have a picture name like example.jpg/png in your res/drawable folder. – Joey Chong Feb 01 '15 at 12:58
  • Furthermore `/res/drawable` does not point inside your apk but to the whole file system. So guaranteed not to work (as intended). – Eugen Pechanec Feb 01 '15 at 12:59
  • As this is a syntax error check for misplaced curly braces. – Eugen Pechanec Feb 01 '15 at 13:00
  • @JoeyChong I am making a picture in this program and want to display it at this point, this picture has a timestamp so i cant write this for one picture only – Tim Feb 01 '15 at 13:12
  • @EugenPechanec I already checked 50 times, there are no braces missing – Tim Feb 01 '15 at 13:12
  • Is "bMap" already defined perhaps? Did you try clean & build? What is your IDE? Can you try opening the file in a different IDE? – Eugen Pechanec Feb 01 '15 at 13:16
  • If you are using a dynamic generated picture, then you shouldn't put the picture at `/res/drawable` folder. That is for static picture and it will generate an id at R class. – Joey Chong Feb 01 '15 at 13:18
  • @EugenPechanec its the first and only time i use bMap and its not defined before. I am using Eclipse IDE for Java Developers with and Android SDK plugin, i haven't tried opening it with a differnt IDE. What are you using? – Tim Feb 02 '15 at 16:11
  • @Tim I've been using Android Studio for some time now, there's a stable release out. The compile times are a bit worse than in eclipse then again it's easier (and more power consuming) to build multi-flavor apps or use annotations... Anyway can you post the whole file here or on pastebin? – Eugen Pechanec Feb 02 '15 at 16:19
  • @EugenPechanec http://pastebin.com/UR4Dy2AM here a like to the whole activity...there is anotherone where i make the picture and save it in a file too but everthing is alride there – Tim Feb 02 '15 at 19:10

1 Answers1

0

In OP's pastebin code I've found the following snippet:

String imageFileName =getIntent().getStringExtra("imageFileName");
//String bildquelle = "R.drawable." + imageFileName;

public class ImageChange extends Activity {          
  ImageView iv = new ImageView(this);
  String bildquelle = "R.drawable." + imageFileName;
  int id = getResources().getIdentifier(bildquelle, "drawable", getPackageName());

  Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
  //Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
  iv.setImageBitmap(bMap);
}

There are a couple of problems with this including:

  1. The code is not inside a method body.
  2. The code is a Activity subclass.
  3. The code improperly calls getResources().getIdentifier(...).

Instead of the above code I suggest MERGING the rest of activity with the following:

public class BildAnzeigen extends ActionBarActivity {

  ImageView iv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bild_anzeigen);
    iv = (ImageView) findViewById(R.id.my_image_view); // replace the id with the one from layout definition

    handleIntent(getIntent());
  }

  private void handleIntent(Intent intent) {
    String imageFileName = intent.getStringExtra("imageFileName");
    int id = getResources().getIdentifier(imageFileName, "drawable", null);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
    iv.setImageBitmap(bMap);
  }
}

Note: This will not work if the images are not compiled within your apk. There's no way to add anything to drawables after building. It just doesn't work that way.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • ok thx that really helped me out...how can I do it with a picture from the SDcard from which I know the name and the path? – Tim Feb 02 '15 at 22:25
  • @Tim see this post http://stackoverflow.com/questions/27662279/why-am-i-getting-incorrect-phone-and-sdcard-memory-information/27662469#27662469 you have to use this approach because the location of sd card varies across devices. It may be `/sdcard`, `/storage/sdcard1`, etc. – Eugen Pechanec Feb 02 '15 at 22:52