从外部文件加载图像时出错

时间:2014-06-10 07:03:35

标签: android imageview

因此,我尝试加载存储在SD卡上的图像并将其显示在图像视图中。 但是,它会产生运行时错误。 这是我的Java代码。

package com.example.cameracapture;

import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.Button;
import android.widget.ImageView;


public class DisplayPage extends ActionBarActivity {

String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/image_001.jpg"; 
File imgFile = new File(path);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_page);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
    ImageView myImage = (ImageView) findViewById(R.id.Display);
    myImage.setImageBitmap(myBitmap);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_page, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_display_page, container,
                false);
        return rootView;
    }
    }
}

这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/stupid_android"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/Display"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="@string/upload" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#0000FF" />

</LinearLayout>

我也知道错误是由于这些行 -

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
    ImageView myImage = (ImageView) findViewById(R.id.Display);
    myImage.setImageBitmap(myBitmap);

因为,当我删除上面三行代码时,空的图像视图会完美显示。我称这个图片错了吗?我该怎么做才能纠正它?也许我不应该在OnCreate方法中使用它?

编辑 - 这是我的错误日志

06-10 12:47:02.796: E/AndroidRuntime(25074): FATAL EXCEPTION: main
06-10 12:47:02.796: E/AndroidRuntime(25074): Process: com.example.cameracapture, PID: 25074
06-10 12:47:02.796: E/AndroidRuntime(25074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cameracapture/com.example.cameracapture.DisplayPage}: java.lang.NullPointerException
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.os.Looper.loop(Looper.java:136)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread.main(ActivityThread.java:5102)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at java.lang.reflect.Method.invoke(Method.java:515)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at dalvik.system.NativeStart.main(Native Method)
06-10 12:47:02.796: E/AndroidRuntime(25074): Caused by: java.lang.NullPointerException
06-10 12:47:02.796: E/AndroidRuntime(25074):    at com.example.cameracapture.DisplayPage.onCreate(DisplayPage.java:41)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.Activity.performCreate(Activity.java:5248)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
06-10 12:47:02.796: E/AndroidRuntime(25074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
06-10 12:47:02.796: E/AndroidRuntime(25074):    ... 11 more

2 个答案:

答案 0 :(得分:1)

显然,错误是由于在OnCreate方法中调用图像。

我创建了一个新方法OnResume,并在那里调用了图像,如下所示 -

@Override
protected void onResume() {
    super.onResume();
    String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/image_001.jpg"; 
    File imgFile = new File(path);

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());                  
    ImageView myImage = (ImageView) findViewById(R.id.Display);
    myImage.setImageBitmap(myBitmap);
}

答案 1 :(得分:0)

onCreate中设置图像是正确的方法,但我建议,因为它是一个静态图像是使用drawable资源目录并直接从XML设置图像

只需将图像放在相应的可绘制目录中,然后将其添加到XML

即可

<ImageView
    android:id="@+id/Display"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:contentDescription="@string/upload"
    android:src="@drawable/image_001"/>

<View
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#0000FF" />

作为旁注,您应该使用PNG图像。