gpt4 book ai didi

java - Android 中的相机 Intent 问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:32:02 27 4
gpt4 key购买 nike

我使用相机 Intent 让用户拍摄照片并将其发送到另一个 Activity 以供进一步使用。我在相机 Intent 的 putExtra 方法中包含了 EXTRA_OUTPUT 部分,这样我就可以获得全尺寸图像而不是缩略图。问题是,在捕获图像后,我被重定向到 MainActivity。当我在文件管理器中看到时,我发现图像已保存。请帮忙。

 CameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
file = new File(pictureImagePath);
outputFileUri = Uri.fromFile(file);
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(camera, REQUEST_IMAGE_CAPTURE);
}
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && null != data) {
try {

b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outputFileUri);
String filename123 = "myphoto.jpg";
FileOutputStream out = this.openFileOutput(filename123, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
b.recycle();
Intent in1 = new Intent(this, ongallery.class);
in1.putExtra("picture", filename123);
startActivity(in1);
}catch(Exception e)
{
Toast.makeText(this,"You got Error!",Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
}


//OnaGallery.java
public class ongallery extends Activity {
public ImageView imgView;
int xDim;
int yDim;
String filename;
public Bitmap finale = null ;
public Bitmap bmp = null;
public Bitmap photoEdit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ongallery);
imgView = (ImageView) findViewById(R.id.imgView);
xDim = imgView.getWidth();
filename = getIntent().getStringExtra("picture");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
Bitmap finalPhoto = decoder(filename,400,400);
imgView.setImageBitmap(finalPhoto);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
}
public Bitmap decoder(String filename, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
String filepath = getFileStreamPath(filename).getPath();
BitmapFactory.decodeFile(filepath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
finale = BitmapFactory.decodeFile(filepath, options);
return finale;
}
int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
int inSampleSize = 1;
if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
final int halfHeight = options.outHeight / 2;
final int halfWidth = options.outWidth / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}

最佳答案

权限相机权限检查类

public class Utility {
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission(final Context context)
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.N)
{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}

现在在你发射相机 Intent 之前检查权限

 boolean permissionCheck = Utility.checkPermission(getActivity());
if (permissionCheck) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}

现在在 onActivityResult() 中看起来像

  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST)
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");

FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
// Toast.makeText(SignUpActivity.this, String.valueOf(destination), Toast.LENGTH_LONG).show();
imageView.setImageBitmap(bitmap);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getActivity(), "Cancelled",
Toast.LENGTH_SHORT).show();
}

}

AndroidMenifest.xml 中的权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

希望对您有所帮助。

关于java - Android 中的相机 Intent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700992/

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