【问题标题】:How To Display Image Taken In A New Activity如何显示在新活动中拍摄的图像
【发布时间】:2015-11-07 00:43:40
【问题描述】:

只是想知道是否有人可以帮助我解决这个问题,因为我是 java 的主要新手。我想做的是在我按下按钮时拍照,然后让它显示在新的活动中。到目前为止,我所做的工作是当我单击相机打开的按钮时,将照片保存到我的画廊,然后将我带回相同的活动。任何帮助表示赞赏!

这是我的 mainactivity.java 文件

package com.example.appname;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends Activity {

private File mFileUri;
private final Context mContext = this;

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

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    mFileUri = getOutputMediaFile(1);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

    // start the image capture Intent
    startActivityForResult(intent, 100);
  }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    if (resultCode == RESULT_OK) {
        if (mFileUri != null) {
            String mFilePath = mFileUri.toString();
            if (mFilePath != null) {
                Intent intent = new Intent(mContext, CamActivity.class);
                intent.putExtra("filepath", mFilePath);
                startActivity(intent);



            }
        }
     }               
 }


 // Return image / video
 private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new   File(Environment.getExternalStorageDirectory(), "DCIM/Camera");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == 1) { // image
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == 2) { // video
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
 }
}

这是我的 activity_main.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.appname.MainActivity" >

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:minHeight="50dp"
    android:minWidth="120dp"
    android:onClick="@id/activity_cam"
    android:text="@string/LetsBegin" />

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp"
    android:src="@drawable/abc_ab_share_pack_mtrl_alpha" />

 </RelativeLayout>

这是我的 CamActivity.java,上面只有一个图像视图,它是我希望在相机保存照片后显示图像的地方

package com.example.appname;

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

import java.io.File;

public class CamActivity extends Activity {

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

    Intent intent = getIntent();
    String filepath = intent.getStringExtra("filepath");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8; // down sizing image as it throws OutOfMemory  Exception for larger images
    filepath = filepath.replace("file://", ""); // remove to avoid  BitmapFactory.decodeFile return null
    File imgFile = new File(filepath);
    if (imgFile.exists()) {
        ImageView imageView = (ImageView) findViewById(R.id.imgphoto);
        Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),  options);
        imageView.setImageBitmap(bitmap);
      }
   }


}

我的 activity_cam.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_cam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.isyourfriendaterrorist.CamActivity" >

<ImageView
    android:id="@+id/imgphoto"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/imageview"/>

</RelativeLayout>

【问题讨论】:

    标签: java android xml eclipse


    【解决方案1】:

    您应该具备以下条件:

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
            if (resultCode == RESULT_OK) {
                if (mFileUri != null) {
                    String mFilePath = mFileUri.toString();
                    if (mFilePath != null) {
                        Intent intent = new Intent(mContext, SecondActivity.class);
                        intent.putExtra("filepath", mFilePath);
                        startActivity(intent);
                    }
                }
            }               
        }
    

    请阅读我对以下问题的示例代码的回答(我使用的是 Android Studio,而不是 Eclipse,但我认为您很容易理解):

    How to move image captured with the phone camera from one activity to an image view in another activity?

    希望这会有所帮助!

    【讨论】:

    • 嘿,我在另一个示例中查看了您的答案,如果您查看上面的代码,则完全更改了我的答案。现在它会打开相机、拍照、保存到 SD 卡并开始新的活动。但它不会在图像视图中显示图片:(请帮忙!
    • 请检查您的项目是否有很多activity_cam.xml文件,如果有,检查它们的内容,如果没有,您是否尝试调试应用程序?此外,尝试使用我从UPDATE WIH FULL SOURCE CODE (NEW PROJECT):END OF NEW PROJECT 的所有源代码创建一个新的空项目
    • 嘿,我现在开始工作了!:) 但现在又出现了另一个问题:(哈哈,当我打开应用程序并通过肖像模式拍照时,它总是水平显示。如果我打开相机在将手机保持在纵向模式然后将手机旋转到水平并拍照时没有任何反应,它会单击但它不会显示图片但是如果我打开应用程序以横向模式拿着手机并拍照,它会保存并正确显示图片。知道如何解决这个问题吗?再次感谢您:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多