我正在制作自定义列表视图,其中我必须添加用户从 sd 卡或相机拍摄的图像。为此,我拍摄一个图像视图和两个按钮。一个按钮从相机拍摄照片,另一个按钮从sd 卡拍摄,然后他们添加图像视图中的图片..现在当我在自定义列表视图中传递该图像 uri 时,第一次添加图像,但第二次应用程序崩溃....如果有人知道答案,请发布它。提前致谢!!
这是代码。这是用于按钮单击..
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 2);
}
});
sdcard.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, 0);
}
});
adapter = new SimpleAdapter(
this,
list12,
R.layout.list_row_1,
new String[] { "name", "address", "location", "image" },
new int[] { R.id.name, R.id.address, R.id.location, R.id.image });
setListAdapter(adapter);
在这之后..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
imgUri = Uri.fromFile(picture);
targetImage.setImageURI(imgUri);
location1 = "Camera";
}
else {
if (resultCode == RESULT_OK) {
targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(targetUri));
//decodeBitmap(bitmap, 4, 4);
targetImage.setImageBitmap(bitmap);
location1 = "Sd card";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后我在列表视图中添加图像
private void populateList() {
HashMap<String, Object> temp11 = new HashMap<String, Object>();
name1 = textname.getText().toString();
address1 = textaddress.getText().toString();
if (name1.equals("") || address1.equals("")) {
String str = "Don't Leave field blank !";
Toast toast = Toast.makeText(LastPage.this,
str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if (location1 == "Sd card") {
textname.setText("");
textaddress.setText("");
temp11.put("name", name1);
temp11.put("address", address1);
temp11.put("location", location1);
temp11.put("image", targetUri);
targetImage.setImageBitmap(null);
list12.add(0, temp11);
adapter.notifyDataSetChanged();
} else if (location1 == "Camera") {
textname.setText("");
textaddress.setText("");
temp11.put("name", name1);
temp11.put("address", address1);
temp11.put("location", location1);
temp11.put("image", imgUri);
targetImage.setImageBitmap(null);
list12.add(0, temp11);
adapter.notifyDataSetChanged();
}
日志猫
04-20 00:03:49.599: E/AndroidRuntime(4129): FATAL EXCEPTION: main
04-20 00:03:49.599: E/AndroidRuntime(4129): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.ImageView.resolveUri(ImageView.java:521)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.ImageView.setImageURI(ImageView.java:305)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:264)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:192)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.AbsListView.obtainView(AbsListView.java:1610)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.ListView.onMeasure(ListView.java:1175)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.os.Looper.loop(Looper.java:130)
04-20 00:03:49.599: E/AndroidRuntime(4129): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-20 00:03:49.599: E/AndroidRuntime(4129): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 00:03:49.599: E/AndroidRuntime(4129): at java.lang.reflect.Method.invoke(Method.java:507)
04-20 00:03:49.599: E/AndroidRuntime(4129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-20 00:03:49.599: E/AndroidRuntime(4129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-20 00:03:49.599: E/AndroidRuntime(4129): at dalvik.system.NativeStart.main(Native Method)
04-20 00:03:58.999: W/dalvikvm(4206): VFY: unable to resolve virtual method 2385: Lcom/korovyansk/android/sample/slideout/SampleActivity;.getActionBar ()Landroid/app/ActionBar;