gpt4 book ai didi

android - 我拍摄的照片没有出现在 ImageView 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:38 26 4
gpt4 key购买 nike

我正在尝试将捕获的快照放在 ImageView 中。对于更大的设备,如 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);
}

回答我的 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>

最佳答案

Replace  mCurrentPhotoPath = "file:" +image.getAbsolutePath(); 
with mCurrentPhotoPath = image.getAbsolutePath();

关于android - 我拍摄的照片没有出现在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21513411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com