我正在尝试制作一个从相机拍摄高质量照片并显示在 imageview 中的程序/因此,如果我尝试在 Android 9 和 android 7 上运行代码,它可以正常工作/但是当我尝试将其放入 android 4.1.1 API 16 它不工作当我在拍照后单击 OK 时相机已经停止所以我认为拍照代码可以但问题是 (public void onActivityResult) 所以请帮帮我
public class MainActivity extends AppCompatActivity {
Intent intent;
Uri fileUri, imageUri;
Button btn_choose_image;
ImageView imageView;
Bitmap bitmap, decoded;
public final int REQUEST_CAMERA = 1;
public final int SELECT_FILE = 1;
String currentImagePath = null;
private static final int IMAGE_REQUEST = 1;
int bitmap_size = 40; // image quality 1 - 100;
int max_resolution_image = 800;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_choose_image = (Button) findViewById(R.id.btn_choose_image);
imageView = (ImageView) findViewById(R.id.image_view);
btn_choose_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
// StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
// StrictMode.setVmPolicy(builder.build());
}
private void selectImage() {
imageView.setImageResource(0);
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent Inatent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Inatent.resolveActivity(getPackageManager()) != null) {
File imageFile = null;
try {
imageFile = getImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.fileprovider", imageFile);
Inatent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(Inatent, REQUEST_CAMERA);
}
}
} else if (items[item].equals("Choose from Library")) {
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
// Result code is RESULT_OK only if the user captures an Image
if (resultCode == Activity.RESULT_OK)
switch (requestCode){
case REQUEST_CAMERA:
imageView.setImageURI(Uri.parse(currentImagePath));
break;
}
}
private void setToImageView(Bitmap bmp) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));
imageView.setImageBitmap(decoded);
}
// Untuk resize bitmap
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
private File getImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName="jpg_"+timeStamp+"_";
File storagDir =getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imaeFile=File.createTempFile(imageName,".jpg",storagDir);
currentImagePath=imaeFile.getAbsolutePath();
return imaeFile;
}
}
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
01-19 18:17:41.379 1930-1936/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)
01-19 18:17:41.723 1930-1930/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
01-19 18:17:41.727 1930-1930/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
01-19 18:17:41.775 1930-1930/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
01-19 18:17:41.779 1930-1930/? E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
01-19 18:17:41.811 1930-1930/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$1', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
01-19 18:17:41.891 1930-1930/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering