• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps App crashes when trying to display captured image in imageview

EBianSri

Lurker
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. Where is the problem? I cannot understand simply what is causing this..

Here is my code:

Code:
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
 
Try this code..It works for me

Code:
public class CameraMain extends AppCompatActivity {

ImageView viewImage;
Button b;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_main);
b=(Button)findViewById(R.id.btnSelectPhoto);
viewImage=(ImageView)findViewById(R.id.imageView);
b.setOnClickListener(new View.OnClickListener() {
[USER=1021285]@override[/USER]
public void onClick(View v) {
selectImage();
}
});
}

public void selectImage()
{

final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};

AlertDialog.Builder builder = new AlertDialog.Builder(CameraMain.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
[USER=1021285]@override[/USER]
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(getApplicationContext(), " mounted ", Toast.LENGTH_LONG).show();

}
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);

} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}


[USER=1021285]@override[/USER]
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
if (f != null) {
Toast.makeText(getApplicationContext(), " not null ", Toast.LENGTH_LONG).show();
}
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);

viewImage.setImageBitmap(bitmap);

String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
viewImage.setImageURI(selectedImage);
Toast.makeText(getApplicationContext(), "Done! ", Toast.LENGTH_LONG).show();

}
}

}

}
 
Last edited:
Back
Top Bottom