Gautam Patadiya
Gautam Patadiya

Reputation: 1411

NullPointerException in android

Here i am try to open photo from folder which i saved in before activity of this program's but here i still face the Exception.

ImageView img = null;
Bitmap bmp = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photoeffect_1);
    Intent i = this.getIntent();
    String s = i.getStringExtra("second");
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

    bmp = BitmapFactory.decodeFile("/sdcard/GWonderPhoto/"+s+".jpg");
    img = (ImageView) findViewById(R.id.imageView1);
    img.setImageBitmap(bmp);
    // TODO Auto-generated method stub

}

Exception :

'02-25 17:07:07.543: E/BitmapFactory(24805): Unable to decode stream: java.io.FileNotFoundException: /sdcard/GWonderPhoto/1424905623242.jpg: open failed: ENOENT (No such file or directory)'

Upvotes: 1

Views: 82

Answers (2)

Amrut Bidri
Amrut Bidri

Reputation: 6360

bmp = BitmapFactory.decodeFile("/sdcard/GWonderPhoto/"+s+".jpg");

change this line to:

bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/GWonderPhoto/"+s+".jpg");

EDIT

also see if following permissions are added in Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

i hope this will help you.

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14398

use

setContentView(R.layout.photoeffect_1);

just after

super.onCreate(savedInstanceState);

i.e. rewrite onCreate method as

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 // TODO Auto-generated method stub
    setContentView(R.layout.photoeffect_1);
    Intent i = this.getIntent();
    String s = i.getStringExtra("second");
    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

    bmp = BitmapFactory.decodeFile("/sdcard/GWonderPhoto/"+s+".jpg");
    img = (ImageView) findViewById(R.id.imageView1);
    img.setImageBitmap(bmp);
}

Upvotes: 3

Related Questions