0

我需要有关此 Android 应用程序的帮助!我刚开始在 Android 上进行开发,对于一个学校的项目,我必须创建一个扫描二维码并拍摄照片的应用程序。

我在拍摄照片时遇到问题,我想拍摄照片,按“勾选按钮”确认,打开一个允许您在 上添加评论的新意图,Image然后通过另一个按钮我将在ArrayAdapter.

我将尝试向您展示有问题的代码部分,如果需要更多内容,请询问!

该方法位于按下按钮启动意图的TakePhoto内部。Fragmentcamera

public void TakePhoto() {

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    int imageNum = 0;
    File imagesFolder = new File(Environment.getExternalStorageDirectory(),"aMuse");
    imagesFolder.mkdirs();
    String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
    File output = new File(imagesFolder, fileName);
    while (output.exists()){
        imageNum++;
        fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        output = new File(imagesFolder, fileName);
    }
    Uri uriSavedImage = Uri.fromFile(output);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

    getActivity().startActivityForResult(intent, 100);
}

onActivityResult里面FragmentActivity包含 2 个Fragment,我需要将拍摄的图像传递给另一个意图,(imageResult)该意图将显示拍摄的照片并允许通过EditText.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    super.onActivityResult(requestCode, resultCode, intent); 

    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    if (scanResult != null) {
        Intent qrResult = new Intent(this, QrResult.class);
        String resultString = scanResult.getContents();
        qrResult.putExtra(EXTRA_MESSAGE, resultString);
        startActivity(qrResult);
    }
    else{
        switch (requestCode) {
        case 100:
            if (resultCode == Activity.RESULT_OK) {
                Intent imageResult = new Intent(this, ImagePreview.class);

                startActivity(imageResult);
            }
        }
    }

在这种情况下,应用程序(据我了解)将照片保存在 SD/aMuse 中,名称为 image_nn.jpg。我需要做的是获取这张照片并将 imageResult 设置为位图。我尝试通过 .putExtra 方法传递 uri,但它崩溃并给出“空指针异常”错误。

4

3 回答 3

0

这是将捕获照片并显示在图像视图中的代码

package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                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);
        }  
    } 
}

这是 XML 资源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
    <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

不要忘记添加权限:

 <uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

如果您在活动结果中收到 null 数据,则使用以下代码:

从相机获取高分辨率图像的代码。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 /*
 Here destination is the File object in which your captured images will be stored
 */
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
/*
Here REQUEST_IMAGE is the unique integer value you can pass it any integer
*/
startActivityForResult(intent, REQUEST_IMAGE);

在此之后实现 onActivityResult 方法,如下所示

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
        // Now check the file which you have pass with the intent to capture.
        // Camera had stored the captured image to the file which you have passed with the intent.
    }
}
于 2013-05-16T11:51:43.233 回答
0

我认为我就此事写的这篇博文对你很有帮助:

指南:Android:将相机活动用于缩略图和全尺寸图像

标题解释了内容。翻阅一遍,如果您阅读后有问题,请告诉我。

对您来说重要的是全尺寸图像部分。

更新: 您现在面临的问题:

 FAILED BINDER TRANSACTION ERROR

与您获取图像的方式无关,而是与图像本身有关,引用 android 开发人员:

 The most common reason for this error is a too large IPC

来源:https ://groups.google.com/forum/#!topic/android-developers/KKEyW6XdDvg/discussion

IPC(代表:进程间通信)基本上是 android 中的进程通信方式,您可以在此处阅读更多信息:

http://www.slideshare.net/jserv/android-internals-30176596

您面临的问题很可能是因为您的图像太大。

回到我发布的指南,你有这个和平的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{  
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) 
{
    //Get our saved file into a bitmap object:
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
   Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
}

}

对于这行代码:

 Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

尝试提供更小的尺寸,例如:

 Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 100, 70);

看看这是否解决了您的问题,然后您可以使用这些参数来获得最佳结果。

于 2013-05-16T11:10:49.207 回答
0

在第一个活动中:

fos = new FileOutputStream(pictureFile);
    fos.write(bArray);
        fos.close();
    Intent i = new Intent(ClickImg.this, CameraAct.class);
    imgPath = Uri.fromFile(output);

    System.out.println("Main Data: " +pathFile);
    i.putExtra("bytedata", pathFile);
    i.putExtra("pname", photoName);

    startActivity(i);
    this.finish();

在第二个活动中:

           String photoName = i.getStringExtra("pname");
Bitmap myBitmap = BitmapFactory.decodeFile(photoName);

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);
于 2013-05-16T11:16:07.727 回答