【发布时间】:2015-11-16 18:10:27
【问题描述】:
我希望你们中的一个可以帮助我解决这个问题。我有 2 个活动。一个带有按钮的按钮,当您单击打开相机时,应该保存图片,然后在第二个活动的 imageview 中显示它。我可以打开应用程序单击按钮保存照片单击确定,它会进入第二个活动,但它不显示图像。但是,如果我在图像视图 src 中添加 ic 启动图标,它会显示绿色的 android 徽标,但是当我删除它时,它只是一个空白页:S
我的 MainActivity.java 文件
package com.MYAPP.apk;
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 android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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);
Button btnbutton1 = (Button) findViewById(R.id.button1);
btnbutton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
}});
// start the image capture Intent
startActivityForResult(getIntent(), 100);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mFileUri = getOutputMediaFile(1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
}
@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);
}
}
}
}
// 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: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.MYAPP.apk.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:text="Let's Start Here!" />
</RelativeLayout>
我的 SecondActivity.java 文件
package com.MYAPP.apk;
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 SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
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.imageView1);
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
imageView.setImageBitmap(bitmap);
}
}
}
我的 activity_second.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: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.MYAPP.apk.SecondActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
【问题讨论】:
标签: java android xml eclipse imageview