因此,我試圖加載存儲在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
使用此鏈接:http://stackoverflow.com/questions/19513112/bitmapfactory-decodefile-and-imageview -strange-thing-in-android – prakash
試試這個,因爲你不需要給文件路徑,你可以使用字符串路徑BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; 位圖myBitmap = BitmapFactory.decodeFile(path,options); – raj
@ raj-我也試過你的方式......它仍然給出相同的錯誤 – Deathstroke