Igo*_*vić 6 camera android gallery android-intent
我正在构建的android应用程序,其中一项功能将是从画廊或从相机拍照。使它与所有设备一起使用时出现问题。我找不到每个Android版本和每个设备的正确解决方案。我觉得我已经遍及整个Internet并尝试了所有找到的代码,但是没有成功使它适用于所有提及的代码。我试过实现从官方的android文档代码,但同样的问题。从画廊中挑选东西似乎很有效,但是相机根本无法正常工作。有人可以给我提示,链接或代码怎么做?我确实不敢尝试这样做。我在android中很新...
我需要画廊和相机的两个意图。
你可以做这样的事情,
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLARY = 2;
Uri outPutfileUri;
Run Code Online (Sandbox Code Playgroud)
//对于像棉花糖os中的运行时渗透
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//Define Button in the Xml file and get them
Button galleryButton= (Button)findViewById(R.id.gallerybutton);
Button cameraButton= (Button)findViewById(R.id.camerabutton);
//Listener's on the button
galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_FROM_GALLARY);
}
});
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Camera permission required for Marshmallow version
// permission has been granted, continue as usual
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
Run Code Online (Sandbox Code Playgroud)
然后在ActivityResult上您将获得图像
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
//pic coming from camera
Bitmap bitmap=null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outPutfileUri);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case PICK_FROM_GALLARY:
if (resultCode == Activity.RESULT_OK) {
//pick image from gallery
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(imgDecodableString);
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
//对于棉花糖设备
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Camera permission required for Marshmallow version
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Callback onRequestPermissionsResult interceptadona Activity MainActivity
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PICK_FROM_CAMERA);
} else {
// permission has been granted, continue as usual
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
//Gallery storage permission required for Marshmallow version
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
Run Code Online (Sandbox Code Playgroud)
//对于Android 7.0,您可以执行类似的操作
ContentValues values = new ContentValues(1);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
outPutfileUri = getActivity().getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20250 次 |
| 最近记录: |