I have an imageview in this app, I am able to capture an image on clicking the imageview, but when I want to display that image on the imageview after capturing, the app is getting crashed. Tried some codes from internet, but the problem still persists. What is the problem? I cannot understand simply what is causing this.. :(
My code is below:
package com.example.newfragment; import java.io.File; import android.support.v4.app.Fragment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class TextFragment extends Fragment implements OnClickListener { TextView text,add; private ImageView img; Button b; int value; String mobile; private static final int CAMERA_REQUEST = 1888 ; Camera camera; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.text_fragment, container, false); add= (TextView)view.findViewById(R.id.address); img= (ImageView)view.findViewById(R.id.img); b= (Button)view.findViewById(R.id.b); img.setOnClickListener( this ); b.setOnClickListener( this ); return view; // //////////data received here//////////// public void change( int txt, String txt1, String txt2 ){ add.setText(txt1 ); value = txt; mobile = txt2; @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.equals(img)) /* Clicking and saving image to sd card*/ // //////////////////////////////////// File file = new File(Environment.getExternalStorageDirectory(), " Frag_list" ); file.mkdirs(); String path = Environment.getExternalStorageDirectory() + " /Frag_list/" +value+ " .jpg" ; File file2= new File(path); Uri outputFileUri = Uri.fromFile( file2 ); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE ); intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri ); startActivityForResult( intent, CAMERA_REQUEST ); // /////////////////////////////////// if (v.equals(b)) /* Calling number*/ // ////////////////////////////////////////////// Uri number = Uri.parse( " tel:" +mobile); Intent callIntent = new Intent(Intent.ACTION_CALL, number); startActivity(callIntent); // ////////////////////////////////////////////// // /setting image in display // ///////////////////////////////////////////////////////// public void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) Bitmap photo = (Bitmap) data.getExtras().get( " data" ); // //////////////////////PROBLEM: APP GETTING CRASHED HERE img.setImageBitmap(photo); // //////////////////*/ And the error I am getting:
10 - 01 13 : 34 : 18 . 850 : W/dalvikvm( 7121 ): threadid=1: thread exiting with uncaught exception (group=0x416159a8) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): FATAL EXCEPTION: main 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=198496, result=- 1 , data=null} to activity {com.example.newfragment/com.example.newfragment.MainActivity}: java.lang.NullPointerException 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread.deliverResults(ActivityThread.java: 3525 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread.handleSendResult(ActivityThread.java: 3568 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread.access$1100(ActivityThread.java: 162 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1412 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.os.Handler.dispatchMessage(Handler.java: 107 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.os.Looper.loop(Looper.java: 194 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread.main(ActivityThread.java: 5371 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at java.lang.reflect.Method.invokeNative(Native Method) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at java.lang.reflect.Method.invoke(Method.java: 525 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 833 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 600 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at dalvik.system.NativeStart.main(Native Method) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): Caused by: java.lang.NullPointerException 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at com.example.newfragment.TextFragment.onActivityResult(TextFragment.java: 118 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java: 153 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.Activity.dispatchActivityResult(Activity.java: 5311 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): at android.app.ActivityThread.deliverResults(ActivityThread.java: 3521 ) 10 - 01 13 : 34 : 18 . 873 : E/AndroidRuntime( 7121 ): ... 11 more 10 - 01 13 : 34 : 20 . 822 : I/Process( 7121 ): Sending signal. PID: 7121 SIG: 9 if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) Bitmap photo = (Bitmap) data.getExtras(). get ( " data" ); /// /////////////////////PROBLEM: APP GETTING CRASHED HERE img.setImageBitmap(photo); current one:
File image = new File (Environment.getExternalStorageDirectory() + " /Frag_list/" +value+ " .jpg" ); if ( image . exists ()){ img.setImageBitmap(BitmapFactory.decodeFile( image .getAbsolutePath()));/////////////setting image in imageview
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •