Reputation: 7031
I want to choose images from the Gallery. Please check the following code.
public class Camera extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
WebView localview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void ChoosePhoto(WebView webview)
{
localview=webview;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);
localview.loadUrl("javascript:ReceivePhoto(\""+bMapArray+"\")");
if (fileis != null)
{
fileis.close();
}
if (bufferedstream != null)
{
bufferedstream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
And included the activity in Manifest file. But after choosing the image, OnActivityResult is not called.
Can anyone please help me???
Upvotes: 0
Views: 749
Reputation: 1135
Let's try this.
Your Camera class:
public class Camera {
private Activity mParentActivity;
private OnPhotoChosenListener mPhotoChosenListener;
// Declare an Iterface for comunicating with Activity
interface OnPhotoChosenListener{
public void onPhotoChosen();
}
public Camera(Activity parentActivity) {
mParentActivity = parentActivity;
}
public void ChoosePhoto(WebView webview)
{
mPhotoChosenListener.onPhotoChosen();
}
Your Activity:
public class MyActivity extends Activity implements OnPhotoChosenListener{
Camera myCamera;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myCamera = new Camera(this);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
FileInputStream fileis=new FileInputStream(selectedImagePath);
BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
byte[] bMapArray= new byte[bufferedstream.available()];
bufferedstream.read(bMapArray);
localview.loadUrl("javascript:ReceivePhoto(\""+bMapArray+"\")");
if (fileis != null)
{
fileis.close();
}
if (bufferedstream != null)
{
bufferedstream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onPhotoChosen(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, Comun.GALLERY_PIC_REQUEST);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
The only thing that rest is how to call
ChooseFoto(Webview webview)
method. This is the part i don't undestand well how to do it. Maybe you found the solution.
EDIT: Code added for implementing an Interface.
Upvotes: 0
Reputation: 1135
This is what i do for picking the images from the Gallery:
Activity launch:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, Comun.GALLERY_PIC_REQUEST);
Capture activity result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
mUriImagen = data.getData();
// Do something
}
}
EDIT: Innecesary code removed.
Upvotes: 1