2014-06-10 36 views
0

因此,我試圖加載存儲在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 
+0

使用此鏈接:http://stackoverflow.com/questions/19513112/bitmapfactory-decodefile-and-imageview -strange-thing-in-android – prakash

+0

試試這個,因爲你不需要給文件路徑,你可以使用字符串路徑BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; 位圖myBitmap = BitmapFactory.decodeFile(path,options); – raj

+0

@ raj-我也試過你的方式......它仍然給出相同的錯誤 – Deathstroke

回答

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圖像。

+0

是的,我很熟悉從可繪製目錄上傳圖像。但是,我希望從外部存儲器路徑上傳它。 也許,有一種方法可以在運行代碼之後將圖像從外部存儲器傳輸到可繪製文件夾? – Deathstroke

+0

我明白你現在要做什麼,所以請在這種情況下忽略我的答案:)我的其他建議是這個'Environment.getExternalStorageDirectory()。getAbsolutePath()'如果不是請告訴我你的活動中哪一行是第41行:D –

+0

移動該代碼也不起作用。 第41行是myImage.setImageBitmap(myBitmap); – Deathstroke

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); 
}