当我使用 ImageView 显示来自 sdcard 的图像时,我遇到了问题。我尝试了许多代码,如下所示,但没有人为我工作,知道活动运行没有任何问题“成功”,但屏幕始终为空且不显示图像。任何的想法。提前致谢。
activity_mainvie.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IMAGEVIE" >
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"/>
</RelativeLayout>
为主要活动尝试的代码是:
public class IMAGEVIE extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagevie);
ImageView iv=(ImageView) findViewById(R.id.imageview);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/life.jpg");
iv.setImageBitmap(bMap);
}
}
第二个代码:
public class IMAGEVIE extends Activity {
private File mFichier;
private Uri fileUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagevie);
ImageView iv=(ImageView) findViewById(R.id.imageview);
File f= new File("/sdcard/life.jpg");
Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());
iv.setImageBitmap(bMap);
}
}
第三个代码:
public class IMAGEVIE extends Activity {
private File mFichier;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagevie);
ImageView iv=(ImageView) findViewById(R.id.imageview);
mFichier = new File(Environment.getExternalStorageDirectory(),"life.jpg");
if(mFichier.exists())
{
iv.setImageURI(Uri.fromFile(mFichier));
}
}
}