I'm writing an app that I need to put an image view of which user has to load an image by click on it. After clicking I'm to give options for user to select whether he intents to load a stored image from the phone itself or take a new shot from it's camera.
This question might be redundant but almost none of the similar questions/issues revealed in here didn't reach what I'm trying to do.
P.s. I'm working on Android API15 with Eclipse 4.2 (JUNO) SDK installed.
Here is the snippet code of main activity which gives me an error:
package test.imgbyte.conerter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.net.Uri;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class FindImgPathActivity extends Activity
{
private Uri mImageCaptureUri;
static final int CAMERA_PIC_REQUEST = 1337;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imgfilepath);
Button camera = (Button) findViewById(R.id.btnLoad);
camera.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imgLoaded);
image.setImageBitmap(thumbnail);
String pathToImage = mImageCaptureUri.getPath();
// pathToImage is a path you need.
// If image file is not in there,
// you can save it yourself manually with this code:
File file = new File(pathToImage);
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
}
Error I get is like this from LogCat:
11-05 19:23:11.777: E/AndroidRuntime(1206): FATAL EXCEPTION: main
11-05 19:23:11.777: E/AndroidRuntime(1206): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data (has extras) }} to activity {test.imgbyte.conerter/test.imgbyte.conerter.FindImgPathActivity}: java.lang.NullPointerException