【问题标题】:NullPointerException in androidAndroid中的NullPointerException
【发布时间】:2015-02-25 11:18:03
【问题描述】:

在这里,我尝试从我在该程序的活动之前保存的文件夹中打开照片,但在这里我仍然面临异常。

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

}

例外:

'02-25 17:07:07.543: E/BitmapFactory(24805): 无法解码流: java.io.FileNotFoundException:/sdcard/GWonderPhoto/1424905623242.jpg: 打开失败:ENOENT(没有这样的文件或目录)'

【问题讨论】:

  • 检查图片路径!
  • 日志显示“(没有这样的文件或目录)”。 @Rami 是对的!检查路径
  • 尝试将decodeFile("/sdcard/GWonderPhoto/"+s+".jpg"); 更改为:decodeFile("file://" +"/sdcard/GWonderPhoto/"+s+".jpg"); 或更改为:decodeFile("content://sdcard/GWonderPhoto/"+s+".jpg");
  • @Josef 仍然无法工作
  • 这里是你的解决方案:stackoverflow.com/questions/15939733/…

标签: java android


【解决方案1】:

使用

setContentView(R.layout.photoeffect_1);

紧接着

super.onCreate(savedInstanceState);

即将onCreate方法重写为

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

【讨论】:

  • @GautamPatadiya 因为你的图片路径错误,正如上面所说的ENOENT (No such file or directory)'
  • 这里仍然面临问题
  • @GautamPatadiya 哪个问题,请说清楚,你的项目清了吗?
  • 先生,首先根据您的建议,我编辑了我的代码,但是在 imageVIew 中没有加载图像之后
  • @GautamPatadiya 有没有异常,请检查图片路径
【解决方案2】:
bmp = BitmapFactory.decodeFile("/sdcard/GWonderPhoto/"+s+".jpg");

将此行更改为:

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

编辑

还要查看Manifest中是否添加了以下权限:

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

希望对你有帮助。

【讨论】:

  • 谢谢,但仍然抛出异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多