【问题标题】:Saving photo in Android to sd card and show them in bitmap in other intent将Android中的照片保存到SD卡并以其他意图的位图显示它们
【发布时间】:2013-05-16 11:05:07
【问题描述】:

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

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

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

TakePhoto 方法在 Fragment 内,按下按钮启动 camera 意图。

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,但它崩溃并给出“空指针异常”错误。

【问题讨论】:

    标签: android android-intent bitmap camera


    【解决方案1】:

    我认为我写的这篇关于这个问题的博文对你真的很有帮助:

    Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

    标题解释了内容。看完后有什么问题告诉我。

    对您来说最重要的是完整尺寸的图像部分。

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

     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(代表:Inter-Process Communication)基本上是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);
    

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

    【讨论】:

    • 您好,我尝试使用您的方法,但它返回 FAILED BINDER TRANSACTION ERROR,您能帮我解决吗?
    • 我使用了你的缩略图方法,效果很好,但我更喜欢全尺寸的图像,如果我按照你的指南进行操作,就会出现上述错误。
    • 好的,现在它正在工作,创建多个图像的任何想法?我创建 image_NN.jpg 之类的图像的方法不再有效。感谢您的帮助!
    【解决方案2】:

    在第一个活动中:

    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);
    

    【讨论】:

      【解决方案3】:

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

      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" />
      

      如果您收到活动结果中的数据为空,请使用以下代码:

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

      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.
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-04
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多