ทำแอพถ่ายภาพครับ แต่ติดปัญหา error เวลาที่จะถ่ายภาพแล้วให้แสดงในหน้านี้ 2 ภาพ
คือ ถ่ายภาพครั้งที่ 1 แสดงใน ImageView1
ถ่ายภาพครั้งที่ 2 แสดงใน ImageView2
รบกวนช่วยแนะนำ หน่อยครับ

-------------------------------------------------------------------------------------------------------
ตัวนี้เป็น code ครับ
-----------------------------------------------------------------------------------------------------
[open_code]public static final int REQUEST_CAMERA = 2;
ImageView imageView;
ImageView imageView2;
Uri uri;
Uri uri2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
Button btnIntent = (Button)findViewById(R.id.btnIntent);
btnIntent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + ".jpg";
File f = new File(Environment.getExternalStorageDirectory()
, "DCIM/Camera/" + imageFileName);
uri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(Intent.createChooser(intent
, "Take a picture with"), REQUEST_CAMERA);
}
});
imageView2 = (ImageView)findViewById(R.id.imageView2);
Button btnIntent2 = (Button)findViewById(R.id.btnIntent2);
btnIntent2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_"+ timeStamp +".jpg";
File f = new File(Environment.getExternalStorageDirectory(),"DCIM/Camera/" + imageFileName);
uri2 = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri2);
startActivityForResult(Intent.createChooser(intent,"Tak a picture with"),REQUEST_CAMERA);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
try {
Bitmap bitmap = Media.getBitmap(cr, uri);
imageView.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext()
, uri.getPath(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK){
getContentResolver().notifyChange(uri2,null);
ContentResolver aa = getContentResolver();
try{
Bitmap bitmap = Media.getBitmap(aa,uri2);
imageView2.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext(),uri2.getPath(),Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
}
[/close_code]
------------------------------------------------------------------------------------
ส่วนตัวนี้เป็น Logcat
------------------------------------------------------------------------------------
[open_code]java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity {com.example.teerapon.takephoto/com.example.teerapon.takephoto.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1471)
at android.os.Parcel.readException(Parcel.java:1419)
at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:486)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1621)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1610)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1590)
at com.example.teerapon.takephoto.MainActivity.onActivityResult(MainActivity.java:85)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
[/close_code]
----------------------------------------------------------------------------------------------------
รบกวนด้วยนะครับ
ทำแอพ ถ่ายภาพแบบหลาย ๆ ภาพแล้วแสดงพร้อมกันแต่ติดปัญหา Error ผู้รู้ช่วยแนะนำด้วยครับ
คือ ถ่ายภาพครั้งที่ 1 แสดงใน ImageView1
ถ่ายภาพครั้งที่ 2 แสดงใน ImageView2
รบกวนช่วยแนะนำ หน่อยครับ
-------------------------------------------------------------------------------------------------------
ตัวนี้เป็น code ครับ
-----------------------------------------------------------------------------------------------------
[open_code]public static final int REQUEST_CAMERA = 2;
ImageView imageView;
ImageView imageView2;
Uri uri;
Uri uri2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
Button btnIntent = (Button)findViewById(R.id.btnIntent);
btnIntent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + ".jpg";
File f = new File(Environment.getExternalStorageDirectory()
, "DCIM/Camera/" + imageFileName);
uri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(Intent.createChooser(intent
, "Take a picture with"), REQUEST_CAMERA);
}
});
imageView2 = (ImageView)findViewById(R.id.imageView2);
Button btnIntent2 = (Button)findViewById(R.id.btnIntent2);
btnIntent2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_"+ timeStamp +".jpg";
File f = new File(Environment.getExternalStorageDirectory(),"DCIM/Camera/" + imageFileName);
uri2 = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri2);
startActivityForResult(Intent.createChooser(intent,"Tak a picture with"),REQUEST_CAMERA);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
try {
Bitmap bitmap = Media.getBitmap(cr, uri);
imageView.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext()
, uri.getPath(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK){
getContentResolver().notifyChange(uri2,null);
ContentResolver aa = getContentResolver();
try{
Bitmap bitmap = Media.getBitmap(aa,uri2);
imageView2.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext(),uri2.getPath(),Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
}
[/close_code]
------------------------------------------------------------------------------------
ส่วนตัวนี้เป็น Logcat
------------------------------------------------------------------------------------
[open_code]java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity {com.example.teerapon.takephoto/com.example.teerapon.takephoto.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1471)
at android.os.Parcel.readException(Parcel.java:1419)
at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:486)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1621)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1610)
at android.content.ContentResolver.notifyChange(ContentResolver.java:1590)
at com.example.teerapon.takephoto.MainActivity.onActivityResult(MainActivity.java:85)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
[/close_code]
----------------------------------------------------------------------------------------------------
รบกวนด้วยนะครับ