我正在尝试开发一个动态创建布局的应用。
在我的布局中,我正在创建一个UploadImage
按钮。
我在另一个java类中创建了OnClickListener
,implements
是“OnClickListener”类,因此可以在我的应用程序的不同位置使用它。
但是,不幸的是,我无法启动相机活动。
以下是我的申请的源代码。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
GenerateViews genView = new GenerateViews();
genView.addQuestionWithUploadImageOption(this, layout);
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
public class GenerateViews {
void addQuestionWithUploadImageOption(final Context context,
LinearLayout layout) {
// TODO Auto-generated method stub
LinearLayout layoutImage = new LinearLayout(context);
layoutImage.setOrientation(LinearLayout.HORIZONTAL);
layoutImage.setWeightSum(1);
layoutImage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageView imageView = new ImageView(context);
imageView.setContentDescription("Image");
imageView.setLayoutParams(new LayoutParams(256, 256));
Button uploadImageButton = new Button(context);
uploadImageButton.setText(R.string.uploadImage);
uploadImageButton.setOnClickListener(new UploadImage());
layoutImage.addView(imageView);
layoutImage.addView(uploadImageButton);
layout.addView(layoutImage);
}
}
从这个GenerateViews
java类开始,我正在调用实际实现OnClickListener的UploadImage
类。
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class UploadImage extends Activity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
一切都很好。我的表单是动态创建的,所有文本等都按我的意愿显示。
即使OnClickListener
正在调用UploadImage.java
类。
但这是我得到的错误:
请尽快帮我解决这个问题。 迫切需要。
谢谢
答案 0 :(得分:2)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_CAMERA)
{
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles())
{
if (temp.getName().equals("temp.jpg"))
{
f = temp;
break;
}
}
try
{
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
ivRecipeImage.setImageBitmap(bm);
path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try
{
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if (requestCode == PICK_IMAGE)
{
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, RecipeActivity.this);
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
ivRecipeImage.setImageBitmap(bm);
}
}
}
答案 1 :(得分:0)
您可以查看此项目here。我没有更新它很长时间;)但它会满足你的需要
<强> STEPS 强>
1.在android中创建一个新项目,右键单击它并选择Android,然后单击底部的add将“Library”项目添加到项目中
2.将这些权限放在您的清单文件中
<uses-features android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
以及应用程序标记
下的此活动<activity android:name="com.camera.library.CameraLibrary"></activity>
3.现在获得类的实例
CameraOptions options = CameraOptions.getInstance(this);
options.takePicture();
关于点击事件的按钮或任何活动呼叫用于启动摄像头
Intent intent = new Intent(this,CameraLibrary.class); startActivityForResult(intent,REQUEST_CODE);
当用户拍照或使用options.getBitmapFile()
方法获取文件路径时,调用options.getFilePath()
方法获取位图图像
创建的图像存储在应用程序的缓存目录中
修改强>
像这样更改你的代码
public class UploadImage extends Activity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private CameraOptions options;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
options = CameraOptions.getInstance(UploadImage.this);
options.takePicture();
options.setRequesCode(CAMERA_REQUEST);
Intent intent = new Intent(UploadImage.this,CameraLibrary.class);
startActivityForResult(intent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
imageView.setImageBitmap(options.getBitmapFile());
}
}
}