Move common logic to AXT

This commit is contained in:
ligi 2015-03-03 03:28:35 +01:00
parent 3adf5e946d
commit a8e3ec678b
3 changed files with 9 additions and 130 deletions

View file

@ -20,6 +20,7 @@ apply plugin: 'spoon'
repositories {
jcenter()
mavenLocal()
}
android {
@ -112,7 +113,7 @@ dependencies {
compile 'com.melnykov:floatingactionbutton:1.2.0'
compile 'com.afollestad:material-dialogs:0.6.3.3'
compile 'org.ligi:AXT:0.32'
compile 'org.ligi:AXT:0.33'
compile 'org.ligi:tracedroid:1.4'
compile 'com.google.guava:guava:18.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'

View file

@ -1,116 +0,0 @@
package org.ligi.passandroid;
import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import org.ligi.axt.AXT;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class ImageFromIntentUriToFileConverter {
final Context context;
public ImageFromIntentUriToFileConverter(final Context context) {
this.context = context;
}
public File extract(Uri selectedImage) {
final String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME};
final Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// some devices (OS versions return an URI of com.android instead of com.google.android
if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider")) {
// use the com.google provider, not the com.android provider.
selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
}
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
// if it is a picasa image on newer devices with OS 3.0 and up
if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")) {
columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (columnIndex != -1) {
// Do this in a background thread, since we are fetching a large image from the web
return getBitmap("image_file_name.jpg", selectedImage);
}
} else { // it is a regular local image file
String filePath = cursor.getString(columnIndex);
// taken from http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework
if (filePath == null && Build.VERSION.SDK_INT >= 19) {
filePath = getFilePathForKITKAT(selectedImage);
}
cursor.close();
return new File(filePath);
}
}
// If it is a picasa image on devices running OS prior to 3.0
else if (selectedImage.toString().length() > 0) {
// Do this in a background thread, since we are fetching a large image from the web
return getBitmap("image_file_name.jpg", selectedImage);
}
return null;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private String getFilePathForKITKAT(Uri selectedImage) {
final String wholeID = DocumentsContract.getDocumentId(selectedImage);
// Split at colon, use second item in the array
final String id = wholeID.split(":")[1];
final String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
final String sel = MediaStore.Images.Media._ID + "=?";
final Cursor innerCursor = context.getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
final int columnIndex = innerCursor.getColumnIndex(column[0]);
if (innerCursor.moveToFirst()) {
return innerCursor.getString(columnIndex);
}
return null;
}
private File getBitmap(String tag, Uri url) {
final File cacheDir = context.getCacheDir();
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
final File f = new File(cacheDir, tag);
try {
final InputStream is = getInputStreamByURL(url);
AXT.at(is).toFile(f);
return f;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private InputStream getInputStreamByURL(Uri url) throws IOException {
if (url.toString().startsWith("content://com.google.android.gallery3d")) {
return context.getContentResolver().openInputStream(url);
} else {
return new URL(url.toString()).openStream();
}
}
}

View file

@ -7,21 +7,16 @@ import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import org.ligi.axt.AXT;
import org.ligi.passandroid.App;
import org.ligi.passandroid.ImageFromIntentUriToFileConverter;
import org.ligi.passandroid.R;
import org.ligi.passandroid.events.PassRefreshEvent;
import org.ligi.passandroid.model.PassImpl;
import java.io.File;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.OnClick;
import static android.app.Activity.RESULT_OK;
public class ImageEditFragment extends Fragment {
@ -74,8 +69,7 @@ public class ImageEditFragment extends Fragment {
App.getBus().post(new PassRefreshEvent(getPass()));
}
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
@ -100,7 +94,7 @@ public class ImageEditFragment extends Fragment {
}
private void extractImage(Intent imageReturnedIntent, String name) {
final File extract = new ImageFromIntentUriToFileConverter(getActivity()).extract(imageReturnedIntent.getData());
final File extract = AXT.at(imageReturnedIntent.getData()).loadImage(getActivity());
try {
Files.copy(extract, new File(getPass().getPath() + "/" + name + PassImpl.FILETYPE_IMAGES));
} catch (IOException e) {