0

我正在尝试在另一个活动图像视图上显示从相机单击的图片,但是将图像从捆绑包中取出到位图中..我收到此错误。我的代码是

package com.example.iwm;

import java.io.File;

import java.text.SimpleDateFormat;


import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
//private static final int RESULT_OK = -1;
private static final int MEDIA_TYPE_IMAGE = 1;

public static Bundle s = null;
//public static String y ;
private Uri fileUri;
private static Intent intent;
private static Intent intent2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

      // create Intent to take a picture and return control to the calling application
     intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(android.provider.MediaStore.ACTION_IMAGE_CAPTURE, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    //onActivityResult(CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE, int resultCode, intent)
}


private Uri getOutputMediaFileUri(int type) {
    // TODO Auto-generated method stub
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "IWMP-Images");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("IWMP-Images", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    }  else {
        return null;
    }

    return mediaFile;
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
           Toast.makeText(this, "Image saved to:\n" + fileUri, Toast.LENGTH_LONG).show();
            s =data.getExtras();

             intent2 = new Intent(MainActivity.this,MainActivity2.class);

             intent2.putExtra("Image", s);

            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            getApplicationContext().startActivity(intent2);



        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
             Toast.makeText(this, "Image NOT saved ", Toast.LENGTH_LONG).show();
        }
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

  }

`

从此我将数据(意图)保存在包 S 中并将其发送到 activty2

我的activity2代码是

  package com.example.iwm;

  import android.os.Bundle;
  import android.app.Activity;
  import android.graphics.Bitmap;

  import android.view.Menu;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;

 public class MainActivity2 extends Activity {
 TextView tx;
 Bitmap bmp;
 ImageView ivUserImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);
    ivUserImage = (ImageView)findViewById(R.id.image);
    tx = (TextView)findViewById(R.id.text1);
    //tx.setText(MainActivity.y);
 Bundle bundle = getIntent().getExtras();
 try
 {
 bmp =(Bitmap)bundle.get("Image");

 }
 catch(Exception e)
 {
     tx.setText(e.toString());
}
 ivUserImage.setImageBitmap(bmp);
 //int  i=10;
}


public void upload(View view)
{

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_activity2, menu);
    return true;
}

  }
4

2 回答 2

1

你可以这样试试吗

像这样从相机中获取图像路径

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {

final String[] imageColumns = { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor imageCursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
            null, null, imageOrderBy);
    if (imageCursor.moveToFirst()) {

        String pathoflasttakenimage = imageCursor.getString(imageCursor
                .getColumnIndex(MediaStore.Images.Media.DATA));
    }
}

像这样在捆绑中传递这个图像路径

Intent intent = new Intent(MainActivity.this, MainActivity2.class);



    Bundle bundle = new Bundle();
    bundle.putString("path", pathoflasttakenimage);

    intent.putExtras(bundle);

    startActivity(intent);

在另一个活动中,使用此图像路径在 imageview 中显示图像,如下所示

Bundle b = getIntent().getExtras();
   String path = b.getString("path");

    BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2; // for 1/2 the image to be loaded
        Bitmap thumb = Bitmap.createScaledBitmap(
                BitmapFactory.decodeFile(path, opts), 96, 96, false);
        ivUserImage.setImageBitmap(thumb);

让我告诉我这种方式你面临什么问题。

于 2013-05-20T06:49:10.023 回答
0

用于在第二个活动中从 Bundlebundle.get("data")bundle.get("Image")获取位图。试试看:

Bundle bundle = getIntent().getExtras();
 try
 {
      // getting error here.
   bmp = (Bitmap)bundle.get("data");   //<< use data instead of Image
   ivUserImage.setImageBitmap(bmp);
 }
 catch(Exception e)
 {
     tx.setText(e.toString());}
 }
于 2013-05-20T07:08:45.537 回答