OnActivityResults到另一个意图

时间:2012-04-27 19:18:13

标签: android image android-intent send android-activity

我使用了一个开关,这样当选择button2时,应用程序允许用户从SD卡中选择一张图像。

在OnActivityResult上,我得到了图像uri,我想获得这个URI文件路径,并在Action_send函数的同一活动中的按钮1上使用它。

public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()){
            case R.id.button1:



                Intent i = new Intent(Intent.ACTION_SEND);
                Uri blah = Uri.parse(selectedImagePath);
                i.setType("image/*");     
                i.putExtra(Intent.EXTRA_STREAM, blah);  
                startActivity(Intent.createChooser(i, "title")); // TODO Auto-generated method stub
                break;
            case R.id.button2:
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, PICK_FROM_FILE);
                break;
        }
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        if(requestCode == PICK_FROM_FILE){
            imageUri = data.getData();
            //gotimage.setImageURI(selectedImageUri);
            selectedImagePath = getPath(imageUri);
    }
}

}

private String getPath(Uri selectedImageUri) {
    // TODO Auto-generated method stub
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(imageUri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

我已经在其他问题上看到了这些人的代码,我看到那个人说使用文件路径,如何使用action_send函数完成?

非常感谢

2 个答案:

答案 0 :(得分:0)

您可以从uri获取文件路径

public void getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
       String filepath= cursor.getString(column_index);
    }

在类级别变量中保护此值,然后在button1上单击将字符串传递给此方法

public void shareThisFile(String path){

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    Uri screenshotUri = Uri.parse(path);

    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, path);
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));
    }

答案 1 :(得分:0)

您可以将文件路径设为:

    public static Uri imgui;

    case R.id.button2:
        Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,  
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
         startActivityForResult(choosePictureIntent, 0); 
    startActivityForResult(choosePictureIntent , 4);
    break;

     @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {  
         imgui = data.getData();  
    }  
}

    button1.setOnClickListener(new OnClickListener() {  
    @Override  
     public void onClick(View v) {  
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, imgui);
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));

     }  
    });