我正在尝试将捕获的快照放在图像视图中。对于像nexus 7和sony xperiac这样的大型设备,它正在捕捉,但图像甚至不会显示,我可以看到RESULT_OK,通过设置一个标志位。那我现在该怎么办?
package gatesapps.blogspot.com;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
import android.annotation.SuppressLint;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String mCurrentPhotoPath;// for photo path
ImageView mImageView;
static boolean flag=false;
TextView tv;
Button test;
static final int REQUEST_TAKE_PHOTO = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=(ImageView)findViewById(R.id.mImageView);
tv=(TextView)findViewById(R.id.tv);
test=(Button)findViewById(R.id.test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressLint("SimpleDateFormat") @SuppressWarnings("unused")
private File createImageFile() throws IOException {
// Create an image file name
@SuppressWarnings("deprecation")
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0, 0, 0));
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public void snap(View view){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
flag=true;
test.setVisibility(View.VISIBLE);
setPic();
}
}
private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
}
Ans my xml是
<ScrollView 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/mImageView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="@drawable/back"
android:contentDescription="preview of the pic"
android:scaleType="fitCenter"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Snap the picture"
android:textColor="#3399FF"
android:onClick="snap"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:text="Click snap the picture to take a pic"
android:id="@+id/tv"
android:textAlignment="center"
android:textColor="#FF0000"
android:textSize="25dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:id="@+id/test"
android:onClick="test"
/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:1)
但仍然是图像视图上图像的1/4 现有的(以前我在imageview中有一个图像,即之前 拍照)
如何定义ID为R.id.mImageView
的ImageView?确保您没有设置背景属性:
<ImageView
android:id="@+id/mImageView"
....
....
android:background="@drawable/originalDrawable" />
而是设置src
属性:
android:src="@drawable/originalDrawable"
原因:background
填充ImageView
。 scaleType
适用于src
drawable。
这可能是你看到旧图像1/4的原因。哦,setImageBitmap(bitmap)
设置了src
。
我不知道为什么big
手机没有显示图片。如果我弄明白的话,我会编辑我的答案。
答案 1 :(得分:1)
Replace mCurrentPhotoPath = "file:" +image.getAbsolutePath();
with mCurrentPhotoPath = image.getAbsolutePath();
答案 2 :(得分:-1)
data
意图结果只是一个缩略图,非常小的位图,旨在放入列表或网格视图中。如果要填充屏幕,则必须从文件中读取大图像。从文件解码到位图时需要缩放选项,否则在大多数设备上您将 outOfMemory 。请参阅此解决方案:https://stackoverflow.com/a/8758494/192373。