https://developer.android.com/training/camera/photobasics.html
我所要做的就是用相机拍照,保存并在ImageView中显示.
我按照上面的android教程继续在行上得到一个错误(一个NullPointerException):
Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile);
Run Code Online (Sandbox Code Playgroud)
我知道我需要在应用程序的清单中配置FileProvider,并且"权限"必须匹配.我不太明白我应该在当局的论点中提出什么.我复制了教程中的所有代码,包括文件res/xml/file_paths.xml.如有必要,请提出任何问题
谢谢!
android android-appwidget android-studio android-fileprovider
我在我的项目中使用https://github.com/jhansireddy/AndroidScannerDemo这个扫描库。
当我捕获图像时,我进入D/skia: --- Failed to create image decoder with message 'unimplemented'控制台并且捕获的图像未设置为Android P 中的图像视图。
同样,当我在Android Q 中测试相同的应用程序时,出现以下错误。
2020-02-05 11:32:23.668 9270-9270/? E/ReviewScreenImpl: onClickOKButton() - Fail to decode captured picture
有人可以帮助解决上述问题。
android android-manifest android-camera android-fileprovider
我试图拍照然后使用照片.这就是我做的.
我的设备是Nexus 6P(Android 7.1.1).
首先,我创建了一个Uri:
Uri mPicPath = UriUtil.fromFile(this, UriUtil.createTmpFileForPic());
//Uri mPicPath = UriUtil.fromFile(this, UriUtil.createFileForPic());
Run Code Online (Sandbox Code Playgroud)
然后,我开始Intent:
Intent intent = ActivityUtils.getTakePicIntent(mPicPath);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, RequestCode.TAKE_PIC);
}
Run Code Online (Sandbox Code Playgroud)
最后,我处理了这个Uri问题onActivityResult:
if (requestCode == RequestCode.TAKE_PIC) {
if (resultCode == RESULT_OK && mPicPath != null) {
Bitmap requireBitmap = BitmapFactory.decodeFile(mPicPath.getPath());
//path is like this: /Download/Android/data/{@applicationId}/files/Pictures/JPEG_20170216_173121268719051242.jpg
requireBitmap.recycle();//Here NPE was thrown.
}
}
Run Code Online (Sandbox Code Playgroud)
在此期间,以下是UriUtil:
public class UriUtil {
public static File createFileForPic() throws …Run Code Online (Sandbox Code Playgroud) android bitmap android-camera-intent android-fileprovider android-7.0-nougat
由于FileProvider的更改,我必须修复我们的Android N应用程序.我基本上已经阅读了关于这个主题的所有内容,但我找不到任何解决方案.
这是我们之前的代码,它从我们的应用程序开始下载,将它们存储在Download文件夹中,并调用一个ACTION_VIEW意图作为soons,DownloadManager告诉他已完成下载:
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Log.d(TAG, "Download commplete");
// Check for our download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (mDownloadReference == referenceId) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(mDownloadReference);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String localUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(localUri);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
if (mimeType != null) {
Intent …Run Code Online (Sandbox Code Playgroud) 我目前正在学习Xamarin.Forms.我目前正在尝试使用Plugin.Media.CrossMedia库实现Camera功能.
实施如下:
public async Task<ImageSource> StartCamera()
{
await CrossMedia.Current.Initialize();
if (Plugin.Media.CrossMedia.Current.IsTakePhotoSupported && CrossMedia.Current.IsCameraAvailable)
{
var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions() { SaveToAlbum=false, SaveMetaData=false});
if (photo != null)
return ImageSource.FromStream(() => { return photo.GetStream(); });
else
return null;
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在执行'TakePhotoAsync'方法时,我收到以下错误.
System.ArgumentException: Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project. …
我正在尝试使用FileProvider方式在Android上轻松下载和共享PDF文件以供其他PDF阅读器应用程序(DropBox,Drive PDF Reader或Adobe Reader)阅读.
但是,我一直得到以下异常:
Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
Run Code Online (Sandbox Code Playgroud)
我首先将URL发送到Android DownloadManager以执行下载,然后使用BroadcastReceiver
我的实施如下:
file_paths.xml
<paths>
<external-files-path
name="my_docs"
path="docs" />
</paths>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
从MainActivity:
public void downloadFile(String urlString) {
Uri uri = Uri.parse(urlString);
String filename = "my_file_name.pdf";
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setTitle("App is Downloading a File");
request.setDescription("Downloading PDF File For App");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(mContext, "docs", filename);
request.allowScanningByMediaScanner();
mDownloadManager.enqueue(request);
}
Run Code Online (Sandbox Code Playgroud)
最后来自我的自定义BroadcastReceiver:
uriString是cursor.getString(uriIndex)采用 …
我正在缓存目录中创建一个文件,我想与其他人共享(通过Gmail/WhatsApp等).我可以使用FileProvider来做到这一点,它适用于WhatsApp.当选择在Gmail上共享时,照片已正确附加,但我通过Intent.EXTRA_STREAM传递的Uri也最终被Gmail解析为新撰写的电子邮件的"收件人:"字段中的地址以及地址( es)我通过Intent.EXTRA_EMAIL传递.
因此,用户需要在发送之前删除伪造(Uri)电子邮件地址.知道如何防止这种情况发生吗?
Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.mypackage.fileprovider", cacheFile);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(contentUri, "image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"some_address@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
intent.putExtra(Intent.EXTRA_TEXT, "Check out this photo");
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
if(intent.resolveActivity(getActivity().getPackageManager()) != null)
{
startActivity(Intent.createChooser(intent, getString(R.string.share_file)));
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个PDF文件并将其保存在本地存储中.当试图打开它时,它在除了Android N之外的所有设备中都是完美的.我可以使用FileProvider在Android N中打开PDF文件,但它显示为空白.
这是我的URI
content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
Run Code Online (Sandbox Code Playgroud)
这是我的代码
Uri path;
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ "Android" + "/" + "data" + "/" + "com.products" + "/" + file);
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
} else {
path = Uri.fromFile(pdfFile);
}
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud) 我FileProvider在我的应用程序中使用。像往常一样,我<Provider>在AndroidManifest.xml文件中声明了标签,如下所示。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.jk.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
当我在具有lollipop版本的android设备上运行它时,它工作正常。当我在kitkat版本上尝试时,它显示以下错误:
FATAL EXCEPTION: main
Process: com.jk.android.perfectphotoeditor2018, PID: 24992
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.jk.android.perfectphotoeditor2018-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.jk.android.perfectphotoeditor2018-2, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:5071)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4648)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4588)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1290)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class …Run Code Online (Sandbox Code Playgroud) android classnotfoundexception android-camera android-fileprovider
我已经审阅了几篇关于此问题的帖子,并且尝试了六种建议的解决方案。我有一些与此工作几乎相同的东西,而不会在我编写的另一个应用程序中引发异常,但我只是无法让它不引发异常,尽管文件确实被传输了!正在设置权限并且文件传输得很好,但仍然会抛出异常,尽管它不会使应用程序崩溃。
我所拥有的是这样的:
fun shareVideo(videoFile: File, context: Context) {
if(videoFile.exists()) {
Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
} else {
Timber.w("Video file does not exist. Exiting.")
return
}
val uris = arrayListOf<Uri>()
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", videoFile)
Timber.i("Uri: $uri + path: ${uri.path}")
uris.add(uri)
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.putExtra(Intent.EXTRA_SUBJECT, "Shared files")
intent.type = "video/mp4"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Timber.i("Intent: $intent")
try{
ContextCompat.startActivity(context, Intent.createChooser(intent, "Shared Videos"), null)
} catch (e: Exception) …Run Code Online (Sandbox Code Playgroud)