簡體   English   中英

我無法將相機拍攝的圖像從一個活動轉移到android中的另一個活動?

[英]I am unable to transfer the image taken from camera from one activity to another activity in android?

我知道如何將攝像機拍攝的圖像從一個活動轉移到另一個活動。但是在第二個活動中不顯示圖像。我知道這很簡單。但是我被卡在這里。請告訴我我在哪里做了我正在通過字節數組傳輸圖像,並且在已傳輸的位圖上出現了空指針異常。

我的第一個活動是

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

 public class Working extends Activity{
ImageView first,second;
Button set;
Bitmap bitmap,scaled;
RelativeLayout relative;
String path;
File file;

protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.working);
 first=(ImageView)findViewById(R.id.imageView1);
 set=(Button)findViewById(R.id.button1);                 
 set.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    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, 1);
}
 });

 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {

            if (requestCode == 1) {

                File f = new File(Environment.getExternalStorageDirectory().toString());

                for (File temp : f.listFiles()) {

                    if (temp.getName().equals("temp.jpg")) {

                        f = temp;

                        break;

                    }

                }

                try {

                    Bitmap bitmap;

                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmapOptions.inSampleSize=2;

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),

                            bitmapOptions); 



                    first.setImageBitmap(bitmap);



                     path = android.os.Environment .getExternalStorageDirectory() + File.separator+ "Phoenix" + File.separator + "default";

                    f.delete();

                    OutputStream outFile = null;

                    file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");

                    try {

                        outFile = new FileOutputStream(file);

                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);

                        outFile.flush();

                        outFile.close();

                    } catch (FileNotFoundException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }               
                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                Intent intent = new Intent(Working.this, Temp.class);
                intent.putExtra("image", byteArray);
                startActivity(intent);

            }
      }
       }

           }

我的第二項活動是

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

  public class Temp extends Activity{
   ImageView iv;
protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.temp);
    iv=(ImageView)findViewById(R.id.imageView2);
        Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("image");

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    iv.setImageBitmap(bitmap);
    }
  }

您不應故意傳遞字節數組,因為如果圖像大小很大,它可能會引起問題。 相反,為什么不按意圖傳遞路徑並在下一個活動中從路徑讀取。

在第一個活動中嘗試這個答案

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    Intent intent = new Intent(firstActivty.this, secondActivity.class);
    intent.putExtra("picture", byteArray);
    startActivity(intent);

然后在第二個活動中,從捆綁中獲取字節數組並轉換為位圖圖像:-

   Bundle extras = getIntent().getExtras();
 byte[] byteArray = extras.getByteArray("picture");

 Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

在這里,您可以選擇通過sd卡的直接路徑來捕獲圖像。

將Byte數組從一個活動傳遞到另一個活動不是一個好習慣。

我認為您應該解碼SD卡的路徑,然后再嘗試。 它應該工作。

1.)首先在字符串的第二個活動中傳遞路徑。

2.)使用密鑰獲取該字符串,並將此邏輯應用於第二個活動。

 File file = new File("your sdcard path");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

我只是編輯您的代碼,只是希望對您有所幫助

Working.java

public class Working extends Activity {
    ImageView first, second;
    Button set;
    Bitmap bitmap, scaled;
    RelativeLayout relative;
    final private int CAPTURE_IMAGE = 2;
    private String imgPath = "";

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.working);
        first = (ImageView) findViewById(R.id.imageView1);
        set = (Button) findViewById(R.id.button1);
        set.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

    }

    public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + System.currentTimeMillis() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }

    public String getImagePath() {
        return imgPath;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == CAPTURE_IMAGE) {
                Intent intent = new Intent(Working.this, Temp.class);
                intent.putExtra("image", getImagePath());
                startActivity(intent);
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

}

臨時庫

public class Temp extends Activity {
    ImageView iv;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.temp);
        iv = (ImageView) findViewById(R.id.imageView2);

        String imagePath = getIntent().getStringExtra("image");

        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        iv.setImageBitmap(bitmap);
    }
}

替換intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f)); 這行與intent.putExtra(“ image”,f.getAbsolutePath());

然后在第二個活動中使用getintent()。getextras.getString(“ image”);獲得該路徑。

就是這樣...我希望能對您有所幫助...

我找到了答案。我在這里寫我的兩個活動。

我的第一個活動是

  import java.io.File;
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;

  public class Working extends Activity{
ImageView first,second;
Button set;
Bitmap bitmap,scaled;
RelativeLayout relative;
String path;
File file;

protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.working);
 first=(ImageView)findViewById(R.id.imageView1);
 set=(Button)findViewById(R.id.button1);                 
  set.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    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, 1);
}
  });

   }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {

            if (requestCode == 1) {

                File f = new File(Environment.getExternalStorageDirectory().toString());

                for (File temp : f.listFiles()) {

                    if (temp.getName().equals("temp.jpg")) {

                        f = temp;

                        break;

                    }

                }



                    Bitmap bitmap;

                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmapOptions.inSampleSize=2;

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),

                            bitmapOptions); 



                    first.setImageBitmap(bitmap);
                    Intent i = new Intent(Working.this, Temp.class);
                    i.putExtra("imagePath" ,f.getAbsolutePath() ); 
                    startActivity(i);
      }
  }
 }
 }

我的第二項活動是

import android.app.Activity;
import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.widget.ImageView;

 public class Temp extends Activity{
 ImageView iv;
protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.temp);
    iv=(ImageView)findViewById(R.id.imageView1);
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

    bitmapOptions.inSampleSize=6;    
    String imagePath = getIntent().getStringExtra("imagePath");

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath,bitmapOptions);
    iv.setImageBitmap(bitmap);
 }
 }

我的第一個活動xml文件和第二個活動xml文件是相同的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relative" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentBottom="true"
    android:text="Button" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher"/>"

</RelativeLayout>

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM