Java 我无法将图像保存在android的外部存储器中

Java 我无法将图像保存在android的外部存储器中,java,android,Java,Android,我的日志如下,请帮助我 4-21 01:29:26.288 21536-21536/? I/art: Late-enabling -Xcheck:jni 04-21 01:29:26.324 21536-21536/? D/TidaProvider: TidaProvider() 04-21 01:29:26.331 21536-21536/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#

我的日志如下,请帮助我

4-21 01:29:26.288 21536-21536/? I/art: Late-enabling -Xcheck:jni
04-21 01:29:26.324 21536-21536/? D/TidaProvider: TidaProvider()
04-21 01:29:26.331 21536-21536/? W/ReflectionUtils: java.lang.NoSuchMethodException: android.os.MessageQueue#enableMonitor()#bestmatch
        at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338)
        at miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375)
        at miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800)
        at miui.util.ReflectionUtils.tryCallMethod(ReflectionUtils.java:818)
        at android.os.BaseLooper.enableMonitor(BaseLooper.java:47)
        at android.os.Looper.prepareMainLooper(Looper.java:111)
        at android.app.ActivityThread.main(ActivityThread.java:5584)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
04-21 01:29:26.363 21536-21536/? W/ResourceType: No package identifier when getting name for resource number 0x00000000
04-21 01:29:26.373 21536-21536/? W/System: ClassLoader referenced unknown path: /data/app/com.example.quotescreator-2/lib/arm64
04-21 01:29:26.410 21536-21536/? W/ResourceType: No package identifier when getting name for resource number 0x00000000
04-21 01:29:26.422 21536-21536/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-21 01:29:26.454 21536-21536/? D/AccessibilityManager: current package=com.example.quotescreator, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=false, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
04-21 01:29:26.480 21536-21536/? I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-21 01:29:26.481 21536-21536/? I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-21 01:29:26.604 21536-21579/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-21 01:29:26.714 21536-21579/? I/Adreno: QUALCOMM build                   : 10c9f68, I74772a33ad
    Build Date                       : 02/07/17
    OpenGL ES Shader Compiler Version: XE031.07.00.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.5.1_RB1.06.00.01.192.038
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
04-21 01:29:26.761 21536-21579/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-21 01:29:28.493 21536-21536/com.example.quotescreator I/Timeline: Timeline: Activity_launch_request time:18521828
04-21 01:29:29.956 21536-21536/com.example.quotescreator D/path: /data/user/0/com.example.quotescreator/app_imageDir/UniqueFileName.jpg
04-21 01:29:41.239 21536-21536/com.example.quotescreator I/Timeline: Timeline: Activity_launch_request time:18534574
04-21 01:29:41.829 21536-21579/com.example.quotescreator D/OpenGLRenderer: endAllStagingAnimators on 0x559e61e8e0 (InsetDrawable) with handle 0x559e5cda60
04-21 01:30:55.099 21536-21536/com.example.quotescreator W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
04-21 01:30:55.125 21536-21536/com.example.quotescreator W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection      

是否确定已检查用户是否已授予外部存储权限

然后试试这个(您的代码不适用于20个API级别或更高级别):

thx来自

    public void SaveImage(View view) {
            // coneverting relative layout in image  to save as we have to save image with text written
            // on it i.e we have to save the whole layout
            Bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            relativeLayout.draw(canvas);
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            File file = new File(directory, "UniqueFileName" + ".jpg");
            if (!file.exists()) {
                Log.d("path", file.toString());
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (java.io.IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
private void saveImageToExternalStorage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
    file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
}
catch (Exception e) {
    e.printStackTrace();
}


// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
});

}