|
|
@ -23,17 +23,19 @@ import android.content.Intent; |
|
|
|
import android.database.Cursor; |
|
|
|
import android.database.Cursor; |
|
|
|
import android.net.Uri; |
|
|
|
import android.net.Uri; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Bundle; |
|
|
|
|
|
|
|
import android.os.ParcelFileDescriptor; |
|
|
|
import android.provider.OpenableColumns; |
|
|
|
import android.provider.OpenableColumns; |
|
|
|
import android.widget.Toast; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
|
|
import org.floens.chan.ChanApplication; |
|
|
|
import org.floens.chan.ChanApplication; |
|
|
|
import org.floens.chan.R; |
|
|
|
import org.floens.chan.R; |
|
|
|
import org.floens.chan.utils.IOUtils; |
|
|
|
import org.floens.chan.utils.IOUtils; |
|
|
|
|
|
|
|
import org.floens.chan.utils.Utils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ImagePickActivity extends Activity { |
|
|
|
public class ImagePickActivity extends Activity { |
|
|
|
private static final int IMAGE_RESULT = 1; |
|
|
|
private static final int IMAGE_RESULT = 1; |
|
|
@ -43,13 +45,14 @@ public class ImagePickActivity extends Activity { |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
|
|
|
intent.setType("image/*"); |
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE); |
|
|
|
|
|
|
|
intent.setType("*/*"); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
startActivityForResult(intent, IMAGE_RESULT); |
|
|
|
startActivityForResult(intent, IMAGE_RESULT); |
|
|
|
} catch (ActivityNotFoundException e) { |
|
|
|
} catch (ActivityNotFoundException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
Toast.makeText(this, R.string.image_open_failed, Toast.LENGTH_LONG).show(); |
|
|
|
Toast.makeText(this, R.string.file_open_failed, Toast.LENGTH_LONG).show(); |
|
|
|
finish(); |
|
|
|
finish(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -62,36 +65,37 @@ public class ImagePickActivity extends Activity { |
|
|
|
if (data != null) { |
|
|
|
if (data != null) { |
|
|
|
final Uri uri = data.getData(); |
|
|
|
final Uri uri = data.getData(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String name = "file"; |
|
|
|
|
|
|
|
|
|
|
|
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null); |
|
|
|
Cursor returnCursor = getContentResolver().query(uri, null, null, null, null); |
|
|
|
if (returnCursor != null) { |
|
|
|
if (returnCursor != null) { |
|
|
|
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
|
|
|
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
|
|
|
returnCursor.moveToFirst(); |
|
|
|
returnCursor.moveToFirst(); |
|
|
|
String name = "image"; |
|
|
|
|
|
|
|
if (nameIndex > -1) { |
|
|
|
if (nameIndex > -1) { |
|
|
|
name = returnCursor.getString(nameIndex); |
|
|
|
name = returnCursor.getString(nameIndex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
returnCursor.close(); |
|
|
|
returnCursor.close(); |
|
|
|
|
|
|
|
} |
|
|
|
final String finalName = name; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ChanApplication.getReplyManager()._onPickedFileLoading(); |
|
|
|
ChanApplication.getReplyManager()._onPickedFileLoading(); |
|
|
|
|
|
|
|
|
|
|
|
// Async load the stream into "pickedFileCache", an file in the cache root
|
|
|
|
final String finalName = name; |
|
|
|
new Thread(new Runnable() { |
|
|
|
new Thread(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
final File cacheFile = new File(getCacheDir() + File.separator + "pickedFileCache"); |
|
|
|
final File cacheFile = new File(getCacheDir() + File.separator + "picked_file"); |
|
|
|
|
|
|
|
|
|
|
|
if (cacheFile.exists()) { |
|
|
|
if (cacheFile.exists()) { |
|
|
|
cacheFile.delete(); |
|
|
|
cacheFile.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
InputStream is = getContentResolver().openInputStream(uri); |
|
|
|
ParcelFileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(uri, "r"); |
|
|
|
FileOutputStream fos = new FileOutputStream(cacheFile); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IOUtils.copy(is, fos); |
|
|
|
FileInputStream is = new FileInputStream(fileDescriptor.getFileDescriptor()); |
|
|
|
|
|
|
|
FileOutputStream os = new FileOutputStream(cacheFile); |
|
|
|
|
|
|
|
IOUtils.copy(is, os); |
|
|
|
|
|
|
|
|
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
runOnUiThread(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -102,12 +106,17 @@ public class ImagePickActivity extends Activity { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Utils.runOnUiThread(new Runnable() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
ChanApplication.getReplyManager()._onPickedFile("", null); |
|
|
|
|
|
|
|
Toast.makeText(ImagePickActivity.this, R.string.file_open_failed, Toast.LENGTH_LONG).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Toast.makeText(this, R.string.image_open_failed, Toast.LENGTH_LONG).show(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}).start(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|