如何在android中的Image View中显示具有固定Uri的图像

时间:2015-07-11 19:52:21

标签: android image android-layout android-imageview

我想在LinearLayout中显示它下面的图像和按钮。对于图像,我使用的是ImageView,2个按钮是嵌套的LinearLayout的一部分。

您可能会想,这已经可以在网上找到了。为什么这个家伙浪费我们的时间?这有什么不同?

不同之处在于要显示的图像的Uri是静态的。我在网上看到的大多数代码示例使用图像选择器来确定图像的Uri。但由于一些限制,我不能使用图像选择器,但可以确保要显示的图像与Uri始终具有相同的固定字符串。

所以,这是我试过的:

activity_main.xml中

return

MainActivity.java

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:id="@+id/image_view"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Start"
        android:id="@+id/start_btn" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Stop"
        android:id="@+id/stop_btn" />

</LinearLayout>

}

如您所见,我尝试加载的图片位于“/sdcard/DCIM/Camera/1.jpg”。但是,这个应用程序一启动就会一直崩溃。 所以,请指导我如何解决这个问题。我是Android编程的新手,如果我对编写这个应用程序的尝试看起来很可悲,请原谅我。

毋庸置疑,在此先感谢:)

编辑:      按照@ Jonty800的要求,这里是Logcat的输出,日志级别设置为“Debug”:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String picturePath = "/sdcard/DCIM/Camera/1.jpg";

    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    setContentView(R.layout.activity_main);
}
// Some auto-generated methods here.

1 个答案:

答案 0 :(得分:1)

我们可以查看崩溃报告吗?请记住,您必须提供访问Manifest.xml文件中的SD卡的权限,这可能是原因。

Form

编辑:

也许尝试这样的事情:

True