当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?
示例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)
日志:
android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外
编辑:
在定位Android Nougat时,file://不再允许使用URI.我们应该使用content://URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?
我希望这不是一个重复的问题,但我正在制作一个应用程序,我想要一个按钮打开相机应用程序(单独的默认Android相机).我怎么做到这一点?我知道有一个功能:
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE)
Run Code Online (Sandbox Code Playgroud)
我需要使用它吗?如何从xml文件中调用该按钮?
此外,我是否需要担心存储该图片或视频,或者默认的相机应用程序会处理这个问题?
我试图从拍摄照片后获得完整的图像Fragment.
如果我使用文件(Uri.fromFile(file))中的Uri,拍摄照片并点击"确定"按钮后相机将不会退出(看起来无法写入Uri或谁知道什么).
使用File String,形式'/data/data/com.package.bla/cache/img198346262jpg',它不能正常工作(文件在那里,但它是空的,因为相机没有保存任何东西).
到目前为止我尝试了什么:
所以我不知道为什么图像没有被保存,并且已经花费/浪费了大量时间测试并弄清楚它为什么不起作用.
分段:
private void launchCamera() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File outputDir = getActivity().getCacheDir();
File file = null;
try {
file = File.createTempFile("img", "jpg", outputDir);
} catch (IOException e) {
e.printStackTrace();
}
if (file != null) {
mImageUri = Uri.fromFile(file); //using Uri is not even exiting the camera
//mImageUri = File.toString(); //If I use String instead of an Uri, it works better …Run Code Online (Sandbox Code Playgroud) 对我来说,实施和学习是一件非常有趣和新鲜的事情.我的申请有一些要求.为此,我搜索了很多,但找不到合适的,有价值的东西或链接.所以我在这里发帖子.功能是:
1)在三星设备中有Safety Assistance菜单,您可以在其中找到Emergency Mode选项.
2)现在Emergency mode我想在那时创建一个或多个主要联系人Emergency Mode.因此,现在在保持主页和音量按钮时,emergency将激活并通知通知.
3)现在emergency将在同一时刻激活GPS将自动启用,并且将自动捕获当前位置的两张照片,最后位置和两张照片将发送到Emergency mode作为主要联系人添加的联系人MMS.
注意:如果将启用录音机选项,则录音文件也将发送MMS.
所以,如果有人知道或有人实施,那么我非常感谢他/她.帮助将不胜感激.
这是我第一次使用相机.我已经阅读了很多例子和文档,所以我试着让自己的班级用前置摄像头拍照.
这就是我想出的:
public class CameraController {
private Context context;
private boolean hasCamera;
private Camera camera;
private int cameraId;
public CameraController(Context c){
context = c.getApplicationContext();
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
cameraId = getFrontCameraId();
if(cameraId != -1){
hasCamera = true;
}else{
hasCamera = false;
}
}else{
hasCamera = false;
}
}
public boolean hasCamera(){
return hasCamera;
}
public void getCameraInstance(){
camera = null;
if(hasCamera){
try{
camera = Camera.open(cameraId);
}
catch(Exception e){
}
}
}
public void takePicture(){
if(hasCamera){
camera.takePicture(null,null,mPicture);
}
}
public void releaseCamera(){
if(camera != …Run Code Online (Sandbox Code Playgroud) 所以我有一个蓝牙设备,我听设备上的按钮按下,并尝试在按下按钮时拍照。问题是我没有找到任何解决方案来做到这一点。
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_OPEN_CAMERA)) {
Intent intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(0, KeyEent.KEYCODE_CAMERA));
context.sendOrderedBroadcast(intent1, null);
intent1 = new Intent("android.intent.action.CAMERA_BUTTON");
intent1.putExtra("android.intent.extra.KEY_EVENT", new KeyEvent(1, KeyEvent.KEYCODE_CAMERA));
context.sendOrderedBroadcast(intent1, null);
} else if (destination.equals(APPLICATION_ACTION_DESTINATION_TAKE_PHOTO)) {
}
Run Code Online (Sandbox Code Playgroud)
我发现做到这一点的唯一方法是使用:
Instrumentation.sendKeyDownUpSync();
Run Code Online (Sandbox Code Playgroud)
问题是我需要注册 INJECT_EVENTS 权限,该权限仅授予系统应用程序。
有人设法做到这一点吗?
android android-intent android-camera android-event android-bluetooth
我在onClick上使用onClick来捕获图像.
PictureCallback myPictureCallback_JPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
FileOutputStream outStream = null;
try {
// Write to SD Card
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(arg0);
outStream.close();
Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}};
Run Code Online (Sandbox Code Playgroud)
图像保存在SD卡中.然后用户单击"发送"按钮并打开布局,在"单击ImageView"上打开图像库并单击特定图像,将选择URI.
imageAttachPhoto = (ImageView)findViewById(R.id.imageViewPhotoOne);
imageAttachPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
} …Run Code Online (Sandbox Code Playgroud)