我将其与我的其他帖子分开:Display image in popout window after button is clicked -- Android/Java
旧线程变得非常复杂和令人困惑,并且有点偏离主题,所以我想创建另一个更清晰的信息。
我正在尝试使用每次运行应用时更改的图像文件路径在Android中显示图像。我知道有一些方法可以在XML布局文件中声明资源,但由于我想要显示的图片是从相机中获取的,所以我不能这样做。我需要能够在不对其进行硬编码的情况下显示图像。
以下是我的代码:
photo_viewer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pictureViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/viewImage" />
</RelativeLayout>
显示图像的Java方法:
public void viewPhoto(String file){
ImageView imageView = new ImageView(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Bitmap image = BitmapFactory.decodeFile(file);
imageView.setImageBitmap(image);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
rl.addView(imageView, lp);
}
然而,当我运行上面的代码时,应用程序崩溃,说它已停止工作。 “file”是一个字符串,表示存储摄像机图像的目录,我知道这部分是正确的。我可以使用它(使用不同的方法)来更改壁纸。它只是显示它导致我的麻烦。我已经尝试了各种方法来简单地显示图像(对话框,“可绘制”,位图工厂等)并且所有这些方法都存在同样的问题。我觉得这很简单,我错过了,因为我是Android应用程序开发的新手。我希望你们中的一个人能够对此有所了解。
修改
我决定尝试另一条路线,但我仍然得到一个空指针异常。这是我的所有代码:
主类,viewPhoto方法:
/* View photo */
public void viewPhoto(String file){
Intent imageView = new Intent(this, PhotoViewer.class);
imageView.putExtra("directory", file);
startActivity(imageView);
}
PhotoViewer类:
package com.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class PhotoViewer extends Activity {
public String file;
ImageView image;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.photo_viewer);
//get file path of image
Bundle extras = getIntent().getExtras();
file = extras.getString("directory");
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
}
}
photo_viewer.xml(使用Eclipse工具创建,因此它不仅仅是任何xml文件)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/photo_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhotoViewer" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/viewImage"/>
</RelativeLayout>
项目清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PhotoViewer"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.androidproject.PhotoViewer" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
该应用程序继续崩溃并告诉我“不幸的是它停止了工作”。这是错误的logcat:
一如既往,感谢您的帮助!
答案 0 :(得分:1)
空指针异常是因为
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
引用一个无效的变量(图像),该变量为空
我最好的猜测是消除你想要启动变量的错误
ImageView image = (ImageView)findViewById(R.id.imageView);