Added Script file chooser. Fixed the issue that background image setting not working
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package com.stardust.scriptdroid.tool;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.stardust.app.OnActivityResultDelegate;
|
||||
import com.stardust.pio.PFile;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/3.
|
||||
*/
|
||||
|
||||
public abstract class DrawableSaver {
|
||||
|
||||
private static final String PREFIX = "saved_drawable_";
|
||||
|
||||
protected Drawable mOriginalDrawble;
|
||||
private Context mContext;
|
||||
private String mName;
|
||||
|
||||
public DrawableSaver(Context context, String name, Drawable originalDrawble) {
|
||||
mContext = context;
|
||||
mName = PREFIX + name;
|
||||
mOriginalDrawble = originalDrawble;
|
||||
readImageAndApply();
|
||||
}
|
||||
|
||||
private void readImageAndApply() {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
InputStream inputStream = mContext.openFileInput(mName);
|
||||
Drawable drawable = BitmapDrawable.createFromStream(inputStream, mName);
|
||||
applyDrawableToView(drawable);
|
||||
} catch (FileNotFoundException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void setDrawable(InputStream inputStream) {
|
||||
saveDrawable(inputStream, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
readImageAndApply();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void saveDrawable(final InputStream inputStream, final Runnable callback) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream outputStream = mContext.openFileOutput(mName, Context.MODE_PRIVATE);
|
||||
PFile.write(inputStream, outputStream);
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
callback.run();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
public void select(Activity activity, final OnActivityResultDelegate.Intermediary intermediary) {
|
||||
new ImageSelector(activity, intermediary, new ImageSelector.ImageSelectorCallback() {
|
||||
@Override
|
||||
public void onImageSelected(ImageSelector selector, InputStream inputStream) {
|
||||
if (inputStream != null)
|
||||
setDrawable(inputStream);
|
||||
intermediary.removeDelegate(selector);
|
||||
}
|
||||
}).select();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
applyDrawableToView(mOriginalDrawble);
|
||||
mContext.deleteFile(mName);
|
||||
}
|
||||
|
||||
public static void reset(Context context, String name) {
|
||||
context.deleteFile(PREFIX + name);
|
||||
}
|
||||
|
||||
protected abstract void applyDrawableToView(Drawable drawable);
|
||||
|
||||
public static class ViewBackgroundSaver extends DrawableSaver {
|
||||
|
||||
private View mView;
|
||||
|
||||
public ViewBackgroundSaver(Context activity, String name, View view) {
|
||||
super(activity, name, view.getBackground());
|
||||
mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyDrawableToView(final Drawable drawable) {
|
||||
mView.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mView.setBackground(drawable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static class ImageSaver extends DrawableSaver {
|
||||
|
||||
private ImageView mView;
|
||||
|
||||
public ImageSaver(Context activity, String name, ImageView view) {
|
||||
super(activity, name, view.getDrawable());
|
||||
mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyDrawableToView(final Drawable drawable) {
|
||||
mView.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mView.setImageDrawable(drawable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ import android.provider.MediaStore;
|
||||
import com.stardust.app.OnActivityResultDelegate;
|
||||
import com.stardust.scriptdroid.R;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/3/5.
|
||||
*/
|
||||
@@ -16,7 +19,7 @@ import com.stardust.scriptdroid.R;
|
||||
public class ImageSelector implements OnActivityResultDelegate {
|
||||
|
||||
public interface ImageSelectorCallback {
|
||||
void onImageSelected(ImageSelector selector, String path);
|
||||
void onImageSelected(ImageSelector selector, InputStream path);
|
||||
}
|
||||
|
||||
private static final int REQUEST_CODE = "LOVE EATING".hashCode() >> 16;
|
||||
@@ -38,19 +41,16 @@ public class ImageSelector implements OnActivityResultDelegate {
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (data == null)
|
||||
if (data == null) {
|
||||
mCallback.onImageSelected(this, null);
|
||||
return;
|
||||
Uri selectedImageUri = data.getData();
|
||||
String[] filePathColumn = {MediaStore.Images.Media.DATA};
|
||||
Cursor cursor = mActivity.getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
|
||||
if (cursor != null) {
|
||||
cursor.moveToFirst();
|
||||
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
|
||||
String filePath = cursor.getString(columnIndex);
|
||||
cursor.close();
|
||||
mCallback.onImageSelected(this, filePath);
|
||||
}
|
||||
|
||||
try {
|
||||
InputStream inputStream = mActivity.getContentResolver().openInputStream(data.getData());
|
||||
mCallback.onImageSelected(this, inputStream);
|
||||
} catch (FileNotFoundException e) {
|
||||
mCallback.onImageSelected(this, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user