Reputation: 167
I want to display an Image and buttons below it in a LinearLayout. For the image, I am using an ImageView and the 2 buttons are part of a nested LinearLayout.
You might be thinking, that this is already available on the Net. Why is this guy wasting our time? What is different in this?
The difference is that the Uri of the Image to be displayed is static. Most of the code examples I have seen on the Net use an Image Picker to determine the Uri of the Image. But due to some constraints, I can't use an Image Picker but can ensure that the Image to be displayed has the same fixed string as Uri always.
So, here is what I tried:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:id="@+id/image_view"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start"
android:id="@+id/start_btn" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop"
android:id="@+id/stop_btn" />
</LinearLayout>
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String picturePath = "/sdcard/DCIM/Camera/1.jpg";
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
setContentView(R.layout.activity_main);
}
// Some auto-generated methods here.
}
As you can see, the image I am trying to load is at "/sdcard/DCIM/Camera/1.jpg". But, this app keeps crashing as soon as it is launched. So, please guide me on how to fix this. And I am a newbie to Android Programming, please pardon me if my attempts at coding this app seem pathetic.
And goes without saying, Thanks in Advance :)
EDIT: As requested by @Jonty800, here's the output of Logcat with the log level set to "Debug":
07-12 02:07:07.937 23773-23773/? D/dalvikvm﹕ Late-enabling CheckJNI
07-12 02:07:08.037 23773-23773/com.example.debayan.usingimageview D/dalvikvm﹕ GC_FOR_ALLOC freed 108K, 36% free 14480K/22584K, paused 17ms, total 17ms
07-12 02:07:08.137 23773-23773/com.example.debayan.usingimageview I/dalvikvm-heap﹕ Grow heap (frag case) to 66.234MB for 52515856-byte allocation
07-12 02:07:08.517 23773-23773/com.example.debayan.usingimageview D/AndroidRuntime﹕ Shutting down VM
07-12 02:07:08.517 23773-23773/com.example.debayan.usingimageview W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417bfd58)
07-12 02:07:08.527 23773-23773/com.example.debayan.usingimageview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.debayan.usingimageview, PID: 23773
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.debayan.usingimageview/com.example.debayan.usingimageview.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.debayan.usingimageview.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 867
Reputation: 522
May we have a look at the crash report? Remember that you must provide permissions to access the SD Card in your Manifest.xml file, this could be the cause.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Edit:
Perhaps try something like this:
String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/1.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(myBitmap);
}else{
System.out.println("Image does not exist");
}
Upvotes: 1