我正在做一个简单的Android相机应用程序,捕获一张图片,然后显示它。捕获部分正在工作,但我不能在imageview.there显示它没有任何错误,但在其中一个logcat声明这
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Root>" +
"<RNC id=\"00\">" +
"<abc1>" +
"</abc1>" +
"<abc2>" +
"</abc2>" +
"</RNC>" +
"<RNC id=\"01\">" +
"<abc3>" +
"</abc3>" +
"<abc4>" +
"</abc4>" +
"</RNC>" +
"<RNC id=\"01\">" +
"<abc5>" +
"</abc5>" +
"<abc6>" +
"</abc6>" +
"</RNC>" +
"</Root>";
XElement root = XElement.Parse(xml);
var groups = root.Elements("RNC").GroupBy(x => x.Attribute("id").Value).ToList();
foreach (var group in groups)
{
if (group.Count() > 1)
{
var all = group.AsEnumerable().ToList();
for (int index = all.Count() - 2; index >= 0; index--)
{
all[index].Add(all[index + 1].Elements());
all[index + 1].RemoveAll();
}
}
}
}
}
}
MainActivity.java
Bitmap too large to be uploaded into a texture (3120x4208, max=4096x4096)
activity_main.xml中
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
public class MainActivity extends Activity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getfile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent,CAM_REQUEST);
}
});
}
private File getfile(){
File folder = new File("sdcard/camera_app");
if(!folder.exists()){
folder.mkdir();
}
File image_file = new File (folder, "cam_image.jpg");
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lala.camera.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="@+id/image_view"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
/>
</LinearLayout>
答案 0 :(得分:0)
您的图片太大,您必须调整它的大小。 试试这个:
imageView.setImageDrawable(resize(Drawable.createFromPath(path)));
private Drawable resize(Drawable image) {
Bitmap bitmap = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap , WIDTH, HEIGHT, false);
return new BitmapDrawable(getResources(), bitmapResized);
}