0

Is it possible to use tinify compression API in Android? I've implemented all the required stuff, but the app is crashing all the time. Here's the code:

File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageName()+".jpg");
    try {
        Log.d("TINY", photo.getAbsolutePath());
        Source source = Tinify.fromFile(photo.getAbsolutePath());
    } catch (IOException e) {
        Log.e("TINY", e.getMessage());
        e.printStackTrace();
    }

A am getting the following error:

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: java.nio.file.Paths

If it's not possible, are there any other good APIs for image compression for Android?

user3330053
  • 173
  • 1
  • 16

2 Answers2

1

It's not possible as-is. Note that java.nio.file.Paths was added in Java 7, but Android still only fully supports Java 6, with some Java 7 language features if you are using a specific buildToolsVersion and minSdkVersion. Also see the Things that don't Work section at the Java7-on-Android project page.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
0

Like the answer to Android import java.nio.file.Files; cannot be resolved states, it's not possible to use classes from the java.nio.file package.

But that doesn't necessarily mean you can't use the Tinify API. If you're able to provide all other referenced classes, you can use it with a few modifications, since it's open source and there are only two occurrences of Files and Paths you need to rewrite:

Result.java

public void toFile(final String path) throws IOException {
    Files.write(Paths.get(path), toBuffer());
}

Source.java

public static Source fromFile(final String path) throws IOException {
    return fromBuffer(Files.readAllBytes(Paths.get(path)));
}
Community
  • 1
  • 1
tynn
  • 38,113
  • 8
  • 108
  • 143