モバイルカメラを使用してその画像を撮影した後、使用して画像を描画しようとすると、FATAL EXCEPTION が発生します。ImageView
例外:
06-04 21:11:10.390: D/AndroidRuntime(19857): Shutting down VM
06-04 21:11:10.414: W/dalvikvm(19857): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-04 21:11:10.437: E/AndroidRuntime(19857): FATAL EXCEPTION: main
06-04 21:11:10.437: E/AndroidRuntime(19857): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.MMS}: java.lang.NullPointerException
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.os.Looper.loop(Looper.java:130)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 21:11:10.437: E/AndroidRuntime(19857): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 21:11:10.437: E/AndroidRuntime(19857): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 21:11:10.437: E/AndroidRuntime(19857): at dalvik.system.NativeStart.main(Native Method)
06-04 21:11:10.437: E/AndroidRuntime(19857): Caused by: java.lang.NullPointerException
06-04 21:11:10.437: E/AndroidRuntime(19857): at com.example.nirbhaya.MMS.onActivityResult(MMS.java:124)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-04 21:11:10.437: E/AndroidRuntime(19857): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-04 21:11:10.437: E/AndroidRuntime(19857): ... 11 more
コード:: パッケージ com.example.nirbhaya;
public class MMS extends Activity implements OnClickListener {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
EditText preLoc,comeby;
Button ok,capture;
String photo;
String dir;
boolean GPS,flag;
String cityName=null;
String SubThorugh = null;
Intent i;
ImageView iv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mms);
preLoc = (EditText)findViewById(R.id.etPreLoc1);
comeby = (EditText)findViewById(R.id.etComing1);
//ok = (Button)findViewById(R.id.bOK1);
capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.btnCapture:
capturePicture();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
Toast.makeText(getApplicationContext(), "photo saved as: "+photo, Toast.LENGTH_LONG).show();
Bitmap photo = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(photo);
}
}
private void capturePicture() {
//here,we are making a folder named picFolder to store pics taken by the camera using this application
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
// here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
count++;
String file = dir+count+".jpg";
photo = file;
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
}
以下のコードを追加した後も、次のエラーが発生します
iv = (ImageView)findViewById(R.id.imageView1);
06-04 22:15:46.515: W/dalvikvm(22711): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-04 22:15:46.546: E/AndroidRuntime(22711): FATAL EXCEPTION: main
06-04 22:15:46.546: E/AndroidRuntime(22711): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.nirbhaya/com.example.nirbhaya.MMS}: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.os.Looper.loop(Looper.java:130)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 22:15:46.546: E/AndroidRuntime(22711): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 22:15:46.546: E/AndroidRuntime(22711): at dalvik.system.NativeStart.main(Native Method)
06-04 22:15:46.546: E/AndroidRuntime(22711): Caused by: java.lang.NullPointerException
06-04 22:15:46.546: E/AndroidRuntime(22711): at com.example.nirbhaya.MMS.onActivityResult(MMS.java:125)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-04 22:15:46.546: E/AndroidRuntime(22711): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-04 22:15:46.546: E/AndroidRuntime(22711): ... 11 more
06-04 22:15:46.921: D/dalvikvm(22711): GC_CONCURRENT freed 268K, 48% free 2956K/5639K, external 583K/945K, paused 3ms+4ms