0

我想从图库中选择图像。请检查以下代码。

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);
    }

}

并将活动包含在清单文件中。但是选择图像后,不会调用 OnActivityResult。

谁能帮帮我吗???

4

2 回答 2

1

这就是我从图库中挑选图像的方法:

活动启动:

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
            intent.setType("image/*");
            startActivityForResult(intent, Comun.GALLERY_PIC_REQUEST);

捕获活动结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK) {
        mUriImagen = data.getData();

             // Do something

    }
}

编辑:删除了不必要的代码。

于 2012-04-25T05:53:35.413 回答
0

让我们试试这个。

你的相机类:

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();
}   

您的活动:

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);
}

唯一剩下的就是怎么打电话

ChooseFoto(Webview webview)

方法。这是我不明白该怎么做的部分。也许你找到了解决方案。

编辑:添加用于实现接口的代码。

于 2012-04-27T11:16:21.440 回答