我正在进行对话,以便能够上传捕获图像的图片,或者从图库中选择。但是,当裁剪图像时,我的应用程序强制关闭。这是我开发的一种方法:
public void uploadPO() {
ivImage1 = (ImageView) findViewById(R.id.iv_image1);
etImage1 = (EditText) findViewById(R.id.et_image1);
ivImage2 = (ImageView) findViewById(R.id.iv_image2);
etImage2 = (EditText) findViewById(R.id.et_image2);
ivImage3 = (ImageView) findViewById(R.id.iv_image3);
etImage3 = (EditText) findViewById(R.id.et_image3);
ivImage4 = (ImageView) findViewById(R.id.iv_image4);
etImage4 = (EditText) findViewById(R.id.et_image4);
final Dialog d = new Dialog(TransDetailActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setBackgroundDrawable(new ColorDrawable((Color.TRANSPARENT)));
d.setContentView(R.layout.upload_po);
final FrameLayout takePicture = (FrameLayout) d.findViewById(R.id.ambil_gambar);
final FrameLayout pilihGambar = (FrameLayout) d.findViewById(R.id.pilih_di_gallery);
final FrameLayout batal = (FrameLayout) d.findViewById(R.id.batal);
final FrameLayout ok = (FrameLayout) d.findViewById(R.id.OK);
takePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectFrom(PICK_FROM_CAMERA);
}
});
pilihGambar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectFrom(PICK_FROM_GALLERY);
}
});
batal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
labelImage1 = etImage1.getText().toString();
labelImage2 = etImage1.getText().toString();
labelImage3 = etImage1.getText().toString();
labelImage4 = etImage1.getText().toString();
if (ivImage1 == null || ivImage2 == null || ivImage3 == null || ivImage4 == null) {
Toast.makeText(getApplicationContext(), "Masukkan Empat Gambar", Toast.LENGTH_SHORT).show();
} else if (labelImage1.trim().equals("") || labelImage2.trim().equals("") || labelImage3.trim().equals("") || labelImage4.trim().equals("")) {
Toast.makeText(getApplicationContext(), "Masukkan Label pada Setiap Gambar", Toast.LENGTH_SHORT).show();
} else {
uploadToApi();
}
}
});
d.show();
}
private void selectFrom(int from) {
if (from == PICK_FROM_CAMERA) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir, "Avatar" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
mImageCaptureUri = Uri.fromFile(output);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, from);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else {
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), from);
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), from);
}
}
}
private void doCrop() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(getApplicationContext(), "Can not find image crop app", Toast.LENGTH_SHORT).show();
intent.setData(mImageCaptureUri);
intent.putExtra("return-data", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("crop", "true");
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
startActivityForResult(intent, CROP_FROM_CAMERA);
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("crop", "true");
intent.putExtra("return-data", true);
ResolveInfo res = list.get(0);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(intent, CROP_FROM_CAMERA);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Toast.makeText(getActivity(),""+resultCode+"", Toast.LENGTH_SHORT).show();
if (data != null) {
if (requestCode == PICK_FROM_CAMERA) {
doCrop();
} else if (requestCode == PICK_FROM_GALLERY) {
mImageCaptureUri = data.getData();
doCrop();
} else if (requestCode == CROP_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
/*Picasso.with(this)
.load()
.transform(new CircleTransform())
.into(imageProfile);*/
if (statusOnUpload == 1) {
ivImage1.setImageBitmap(photo);
} else if (statusOnUpload == 2) {
ivImage2.setImageBitmap(photo);
} else if (statusOnUpload == 3) {
ivImage3.setImageBitmap(photo);
} else if (statusOnUpload == 4) {
ivImage4.setImageBitmap(photo);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] b = baos.toByteArray();
if (statusOnUpload == 1) {
encodedImageString1 = Base64.encodeToString(b, Base64.NO_WRAP);
Log.d(TAG, encodedImageString1.toString());
} else if (statusOnUpload == 2) {
encodedImageString2 = Base64.encodeToString(b, Base64.NO_WRAP);
} else if (statusOnUpload == 3) {
encodedImageString3 = Base64.encodeToString(b, Base64.NO_WRAP);
} else if (statusOnUpload == 4) {
encodedImageString4 = Base64.encodeToString(b, Base64.NO_WRAP);
}
//Log.i("")
//Toast.makeText(getApplicationContext(),""+encodedImageString+"",Toast.LENGTH_SHORT).show();
//DialogDeal dialogDeal=new DialogDeal(EditProfileActivity.this,"imageBase64",encodedImageString,"Cancel");
//dialogDeal.show();
} else { //LogManager.logI("extras == null");}}}}
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/corner_white">
<TextView
android:id="@+id/tittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Upload Purchase Order"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue" />
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/tittle"
android:layout_marginTop="20dp"
android:background="@color/blue" />
<LinearLayout
android:id="@+id/menu_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/line"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/ambil_gambar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Ambil Gambar"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
<View
android:id="@+id/line2"
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_below="@+id/depan"
android:background="@color/grey" />
<FrameLayout
android:id="@+id/pilih_di_gallery"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Pilih di Gallery"
android:textAppearance="?android:attr/textAppearanceSmall" />/>
</FrameLayout>
</LinearLayout>
<View
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@id/menu_upload"
android:background="@color/blue" />
<TableLayout
android:id="@+id/gambar_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/line3"
android:shrinkColumns="*"
android:stretchColumns="*"
android:columnCount="2"
android:rowCount="2">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image1"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:src="@mipmap/addmorepicture"
android:layout_gravity="left|center_vertical" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#809E9E9E"
android:layout_alignBottom="@id/iv_image1"
android:layout_alignLeft="@id/iv_image1"
android:layout_alignStart="@id/iv_image1"
android:layout_alignRight="@id/iv_image1"
android:layout_alignEnd="@id/iv_image1"
android:layout_gravity="center_horizontal|bottom">
<EditText
android:id="@id/et_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Label Image 1"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_image2"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image2"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:src="@mipmap/addmorepicture" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#809E9E9E"
android:layout_alignBottom="@id/iv_image2"
android:layout_alignLeft="@id/iv_image2"
android:layout_alignStart="@id/iv_image2"
android:layout_alignRight="@id/iv_image2"
android:layout_alignEnd="@id/iv_image2">
<EditText
android:id="@id/et_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Label Image 2"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</FrameLayout>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_image3"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image3"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:src="@mipmap/addmorepicture"
android:layout_gravity="left|center_vertical" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#809E9E9E"
android:layout_alignBottom="@id/iv_image3"
android:layout_alignLeft="@+id/iv_image3"
android:layout_alignStart="@+id/iv_image3"
android:layout_alignRight="@+id/iv_image3"
android:layout_alignEnd="@+id/iv_image3"
android:layout_gravity="center_horizontal|bottom">
<EditText
android:id="@id/et_image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Label Image 3"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_image4"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image4"
android:layout_width="180dp"
android:layout_height="120dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:src="@mipmap/addmorepicture" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#809E9E9E"
android:layout_alignBottom="@id/iv_image4"
android:layout_alignLeft="@id/iv_image4"
android:layout_alignStart="@id/iv_image4"
android:layout_alignRight="@id/iv_image4"
android:layout_alignEnd="@id/iv_image4">
<EditText
android:id="@id/et_image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Label Image 3"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</FrameLayout>
</RelativeLayout>
</TableRow>
</TableLayout>
<View
android:id="@+id/line4"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/gambar_upload"
android:background="@color/blue" />
<LinearLayout
android:id="@+id/aksi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/line4"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/batal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="Batal"
android:textAppearance="?android:attr/textAppearanceSmall" />
</FrameLayout>
<View
android:id="@+id/line5"
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_below="@+id/depan"
android:background="@color/grey" />
<FrameLayout
android:id="@+id/OK"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="OK"
android:textAppearance="?android:attr/textAppearanceSmall" />/>
</FrameLayout>
</LinearLayout>
然后,这是错误的迹象:
02-06 11:01:34.836 11791-11791/blablabla.wkwk E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { (has extras) }} to activity {blablabla.wkwk/codelabs.baf.TransDetailActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3488)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3531)
at android.app.ActivityThread.access$1100(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at blablabla.wkwk.TransDetailActivity.onActivityResult(TransDetailActivity.java:531)
at android.app.Activity.dispatchActivityResult(Activity.java:5371)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3484)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3531)
at android.app.ActivityThread.access$1100(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
注意,我只是从另一个类中重新编写这个方法而没有对话框,这是有用的。有人能帮助我吗?
答案 0 :(得分:0)
看起来您只需要在对话框上调用d.setContentView()
,并在调用 public void uploadPO() {
final Dialog d = new Dialog(TransDetailActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setBackgroundDrawable(new ColorDrawable((Color.TRANSPARENT)));
d.setContentView(R.layout.upload_po);
final FrameLayout takePicture = (FrameLayout) d.findViewById(R.id.ambil_gambar);
final FrameLayout pilihGambar = (FrameLayout) d.findViewById(R.id.pilih_di_gallery);
final FrameLayout batal = (FrameLayout) d.findViewById(R.id.batal);
final FrameLayout ok = (FrameLayout) d.findViewById(R.id.OK);
ivImage1 = (ImageView) d.findViewById(R.id.iv_image1);
etImage1 = (EditText) d.findViewById(R.id.et_image1);
ivImage2 = (ImageView) d.findViewById(R.id.iv_image2);
etImage2 = (EditText) d.findViewById(R.id.et_image2);
ivImage3 = (ImageView) d.findViewById(R.id.iv_image3);
etImage3 = (EditText) d.findViewById(R.id.et_image3);
ivImage4 = (ImageView) d.findViewById(R.id.iv_image4);
etImage4 = (EditText) d.findViewById(R.id.et_image4);
//........................
后将所有通话置于:
TABLE `books`
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT
`title` varchar(25) COLLATE utf8_unicode_ci NOT NULL DEFAULT ''
`author` varchar(25) COLLATE utf8_unicode_ci NOT NULL DEFAULT ''
PRIMARY KEY (`id`)
UNIQUE KEY `title` (`title`)
TABLE `chapters`
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT
`book_id` smallint(5) unsigned NOT NULL DEFAULT '0'
`number` smallint(5) unsigned NOT NULL DEFAULT '0'
`title` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT ''
PRIMARY KEY (`id`)
KEY `book_id` (`book_id`) -> Foreign on `books`.`id`
TABLE `pages`
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT
`book_id` smallint(5) unsigned NOT NULL DEFAULT '0'
`chapter_id` smallint(5) unsigned NOT NULL DEFAULT '0'
`number` smallint(5) unsigned NOT NULL DEFAULT '0' // real number, for page 1 of book 1 this has value 1, for page 1 of book 2 this has value 1...
`value` float NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
KEY `book_id` (`book_id`) -> Foreign on `books`.`id`
KEY `chapter_id` (`chapter_id`) -> Foreign on `chapters`.`id`