- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用以下代码从手机相机拍照:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera" />
</LinearLayout>
MainActivity.java:
public class CameraDemoActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here, we are making a folder named picFolder to store
// pics taken by the camera using this application.
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
}
我还将这些权限添加到了 Manifest.xml 中:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但我收到此错误消息:
android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/picFolder/1.jpg exposed beyond app through ClipData.Item.getUri()
问题是什么?如何解决?
编辑:这是更新后的代码:
主要 Activity :
package com.m.textdetection;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private MyTessOCR mTessOCR = new MyTessOCR(MainActivity.this);
private Button takePictureButton;
private ImageView imageView;
/*
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePictureButton = (Button) findViewById(R.id.button_image);
imageView = (ImageView) findViewById(R.id.imageview);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
takePictureButton.setEnabled(false);
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
takePictureButton.setEnabled(true);
}
}
}
public void takePicture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(File);
}
}
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
*/
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
// Uri outputFileUri = Uri.fromFile(newfile);
Uri outputFileUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
private void doOCR()
{
// String temp = mTessOCR.getOCRResult(bitmap);
}
}
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.m.textdetection">
<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"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource"/>
</provider>
</application>
</manifest>
build.gradle(应用程序):
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.m.textdetection"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.rmtheis:tess-two:6.0.3'
compile 'com.android.support:support-v4:27.0.2'
}
这是新的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
出现在这一行:
Uri outputFileUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, newfile);
埃迪尔2:
package com.m.textdetection;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import com.googlecode.tesseract.android.TessBaseAPI;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyTessOCR {
private String datapath;
private TessBaseAPI mTess;
Context context;
public MyTessOCR(Context context)
{
this.context = context;
datapath = Environment.getExternalStorageDirectory() + "/ocrctz/";
File dir = new File(datapath + "/tessdata/");
File file = new File(datapath + "/tessdata/" + "eng.traineddata");
if (!file.exists())
{
Log.d("mylog", "in file doesn't exist");
dir.mkdirs();
copyFile(context);
}
mTess = new TessBaseAPI();
String language = "eng";
mTess.init(datapath, language);
//Auto only
mTess.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_ONLY);
}
public void stopRecognition() {
mTess.stop();
}
public String getOCRResult(Bitmap bitmap)
{
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}
public void onDestroy()
{
if (mTess != null)
mTess.end();
}
private void copyFile(Context context)
{
AssetManager assetManager = context.getAssets();
try
{ InputStream in = assetManager.open("eng.traineddata");
OutputStream out = new FileOutputStream(datapath + "/tessdata/" + "eng.traineddata");
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer); }
} catch (Exception e)
{
Log.d("mylog", "couldn't copy with the following error : "+e.toString());
}
}
}
最佳答案
如果您的 targetSdkVersion >= 24,那么我们必须使用 FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用程序可以访问它们。
1)首先在AndroidManifest.xml标签下添加FileProvider标签,如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource"/>
</provider>
</application>
</manifest>
2) 然后在 res/xml 文件夹中创建一个provider_paths.xml 文件。如果文件夹不存在,则可能需要创建。
<paths>
<external-path name="external_files" path="."/>
</paths>
3)现在编辑您的 Activity 类文件,如下所示:
Uri outputFileUri = Uri.fromFile(newfile);
至
Uri outputFileUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, newfile);
使用以下内容更新您的 MainActivity.java:
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// private MyTessOCR mTessOCR = new MyTessOCR(MainActivity.this);
private Button takePictureButton;
private ImageView imageView;
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkPermissions()){
// permissions granted.
}
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
if (!newdir.exists()) {
newdir.mkdir();
}
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
// Uri outputFileUri = Uri.fromFile(newfile);
Uri outputFileUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
public static final int MULTIPLE_PERMISSIONS = 10; // code you want.
String[] permissions= new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p:permissions) {
result = ContextCompat.checkSelfPermission(MainActivity.this,p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),MULTIPLE_PERMISSIONS );
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MULTIPLE_PERMISSIONS:{
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// permissions granted.
} else {
// Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
// .show();
}
// permissions list of don't granted permission
}
return;
}
}
private void doOCR()
{
// String temp = mTessOCR.getOCRResult(bitmap);
}
}
关于android.os.FileUriExposedException : file:///storage/emulated/0/Pictures/picFolder/1. jpg 通过 ClipData.Item.getUri() 暴露在应用程序之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48990967/
嗨,当尝试将图像上传到 firebase 存储时,我正在使用 firebase 文档,但是出现此错误。 在范围内找不到“存储” let storage = Storage.storage() le
我最近在使用 Firebase 存储时遇到了一些问题。 当我们尝试访问刚刚上传的文件时,浏览器中出现此错误消息 { "error": { "code": 400,
这些是在不同版本的 NuGet 包之间迁移的重要指南: https://github.com/Azure/azure-sdk-for-net/blob/Azure.Storage.Blobs_12.6
警告: Warning: Can't resolve all parameters for Storage in /Users/zzm/Desktop/minan/node_modules/@ioni
我在圆形立方体中收到此错误(“连接到存储服务器失败”)行。我已经检查了所有内容,配置和数据库用户名密码,服务器详细信息都是干净的。谁能告诉我可能是什么问题。这里我给出了整个配置文件。
我希望能够限制容器的大小,但是使用默认的存储驱动程序aufs(对于Ubuntu 14.04),当我尝试使用--storage-opt参数时出现错误 $ docker create -it --name
我希望能够支持对使用 Google Cloud Storage 托管的静态 Assets 进行 Brotli 和 Gzip 编码。为此,我想在将文件上传为 之前对其进行编码, .gz和 .br .问
场景 我有几个由 Google Cloud Storage object.finalize 事件触发的 Google Cloud Functions。为此,我使用两个存储桶并使用“同步选项:覆盖目标位
我在 Google Cloud Storage 中有一个存储桶和一个网站。人们目前可以通过网站上传到存储桶(使用 Google 身份验证)。 但是,我需要设置它以便任何人都可以查看上传的文件(并且不能
如果文件被放入 Google Cloud 存储并公开,但该文件的网址在另一个网页上不存在,那么 Google 是否会在其搜索结果中将其编入索引?有人知道吗? 最佳答案 Google 的搜索索引独立于其
截至今天早上,我无法访问我的存储桶。 当我在导航上选择 Google Cloud Storage 选项卡时,一切都按预期加载,但不是显示我的两个存储桶,而是显示一个警告栏说: We were unab
我想弄清楚是否可以在 Windows 平台上使用 gsutil 的 cp 命令将文件上传到 Google Cloud Storage。我的本地计算机上有 6 个文件夹,每天都会向其中添加新的 pdf
我最近开始使用 Google Cloud Storage。最初我在安装 Cloud SDK 时创建了一个虚拟项目。现在我正在做另一个项目。 gsutil 仍然指向我以前的项目。我如何使它指向我的新项目
我目前正在这样做,但它非常慢,因为我的存储桶中有几 TB 的数据: gsutil du -sh gs://my-bucket-1/ 对于子文件夹也是如此: gsutil du -sh gs://my-
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
将文件上传到 Google Cloud Storage 时,有一个自定义数据字段元数据。 Google's example相当短: var metadata = { contentType: 'a
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
我有一个包含超过 2 万个文件名的 Google Storage 存储桶。有没有办法在短时间内列出存储桶中的所有文件名? 最佳答案 这取决于您所说的“短”是什么意思,但是: 您可以做的一件事来加快列出
有谁知道如果文件不存在,您是否需要为 Google Cloud Storage 中的文件请求付费?换句话说,有人访问您存储桶中不存在的文件是否计入您的请求?还是仅适用于存在的文件? 最佳答案 客户无需
在每一分钟结束时,我的代码总共会上传 20 到 40 个文件(从多台机器上并行上传大约 5 个文件,直到全部上传完毕)到 Google Cloud Storage。我经常收到 429 - Too Ma
我是一名优秀的程序员,十分优秀!