我一直在使用下面的代碼來設置在Android的JPEG圖像的DPI。 當打開我的應用程序時,它應該崩潰,就像不幸的應用程序已停止。 這裏是我的代碼:如何解決java.lang.NoClassDefFoundError:sun.awt.AppContext在android
import ae.java.awt.image.BufferedImage;
import ae.javax.imageio.IIOImage;
import ae.javax.imageio.ImageIO;
import ae.javax.imageio.ImageTypeSpecifier;
import ae.javax.imageio.ImageWriter;
import ae.javax.imageio.metadata.IIOInvalidTreeException;
import ae.javax.imageio.metadata.IIOMetadata;
import ae.javax.imageio.stream.ImageOutputStream;
private void setDPIJPEG() {
int dpi = 100;
File infile = new File(ScanConstants.IMAGE_RESULTS,"grayImages.jpg");
File outfile = new File(ScanConstants.IMAGE_RESULTS,"density.jpg");
try {
BufferedImage sourceImage = ImageIO.read(infile);
ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
imageWriter.setOutput(ios);
IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(sourceImage), null);
Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
jfif.setAttribute("resUnits", "1"); // density is dots per inch
data.mergeTree("javax_imageio_jpeg_image_1.0", tree);
imageWriter.write(data, new IIOImage(sourceImage, null, data), null);
ios.close();
imageWriter.dispose();
} catch (IIOInvalidTreeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我有以下錯誤:
09-13 16:29:30.548 29368-29368/com.scanner.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.scanner.demo, PID: 29368
java.lang.NoClassDefFoundError: sun.awt.AppContext
at ae.javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:155)
at ae.javax.imageio.ImageIO.<clinit>(ImageIO.java:65)
at com.scanlibrary.ResultFragment.setDPIJPEG(ResultFragment.java:269)
at com.scanlibrary.ResultFragment.access$600(ResultFragment.java:46)
at com.scanlibrary.ResultFragment$1.onClick(ResultFragment.java:129)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19274)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
這裏是我的build.gradle文件
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk
{
moduleName "Scanner"
}
multiDexEnabled true
}
sourceSets.main
{
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.github.beyka:androidtiffbitmapfactory:0.9.7.6'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(path: ':openCVLibrary310')
compile 'com.android.support:multidex:1.0.1'
compile files('libs/ae-awt.jar')
}
這裏是掃描庫是在我的應用程序一個模塊。 如何解決它。 如果有人知道,請幫助我。 我正在使用Android Studio 2.3.3 java 1.8。
AWT不適用於Android – EpicPandaForce
您可以發佈您的gradle文件 –