android用ImageView从SD卡读取图片 各位帮我看看这个程序,一运行就崩溃
publicclassImportpicActivityextendsActivity{@OverridepublicvoidonCreate(BundlesavedIn...
public class ImportpicActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView jpgView = (ImageView)findViewById(R.id.jpgview);
Bitmap bm = BitmapFactory.decodeFile("/sdcard/rtest.jpg");
jpgView.setImageBitmap(bm); }
}
log
08-19 00:47:17.496: WARN/dalvikvm(1535): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
08-19 00:47:17.506: ERROR/AndroidRuntime(1535): Uncaught handler: thread main exiting due to uncaught exception
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.pic/cn.pic.ImportpicActivity}: java.lang.NullPointerException
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.os.Looper.loop(Looper.java:123)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at dalvik.system.NativeStart.main(Native Method)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): Caused by: java.lang.NullPointerException
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at cn.pic.ImportpicActivity.onCreate(ImportpicActivity.java:22)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): ... 11 more 展开
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView jpgView = (ImageView)findViewById(R.id.jpgview);
Bitmap bm = BitmapFactory.decodeFile("/sdcard/rtest.jpg");
jpgView.setImageBitmap(bm); }
}
log
08-19 00:47:17.496: WARN/dalvikvm(1535): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
08-19 00:47:17.506: ERROR/AndroidRuntime(1535): Uncaught handler: thread main exiting due to uncaught exception
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.pic/cn.pic.ImportpicActivity}: java.lang.NullPointerException
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.os.Looper.loop(Looper.java:123)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at java.lang.reflect.Method.invoke(Method.java:521)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at dalvik.system.NativeStart.main(Native Method)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): Caused by: java.lang.NullPointerException
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at cn.pic.ImportpicActivity.onCreate(ImportpicActivity.java:22)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-19 00:47:17.516: ERROR/AndroidRuntime(1535): ... 11 more 展开
2个回答
展开全部
我帮你运行了一下你的代码,没有问题啊
setContentView(R.layout.main);
ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeFile("/sdcard/pic2.png");
jpgView.setImageBitmap(bm);
你检查一下,你的main.xml定义的ImageView
我的<ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" ></ImageView>
还有你的sdcard下面的图片是否导入了rtest.jpg
检查后看是否还有问题,如果有,我只有怀疑你是不是强制转换了图片的后缀名,那是不行的。
setContentView(R.layout.main);
ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeFile("/sdcard/pic2.png");
jpgView.setImageBitmap(bm);
你检查一下,你的main.xml定义的ImageView
我的<ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" ></ImageView>
还有你的sdcard下面的图片是否导入了rtest.jpg
检查后看是否还有问题,如果有,我只有怀疑你是不是强制转换了图片的后缀名,那是不行的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询