将用户库上传的图像传递给另一个活动

时间:2017-06-30 11:35:04

标签: android image gallery

我有一个注册活动,用户可以从他的画廊上传图片,我想将这张照片传递给主要活动中的导航抽屉。还有userprofile活动,我无法找到如何,RegistrationActivity的代码:

RegistrationActivity的代码:

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
de.hdodenhof.circleimageview.CircleImageView uplo;
private static int RESULT_LOAD_IMAGE = 1;
Context ctx = this;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    final Button bRegister = (Button) findViewById(R.id.bRegister);
    //upload profile image
    uplo = (de.hdodenhof.circleimageview.CircleImageView)       findViewById(R.id.uploadimage);
    uplo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });



    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                Intent i = new Intent(RegisterActivity.this,MainActivity.class);
              i.putExtra("picture", String.valueOf(uplo));
                startActivity(i);
                finish();


        }

    });

}
//to take the chosen image from user mobile gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        uplo.setImageBitmap(BitmapFactory.decodeFile(picturePath));


    }


}

代码示例非常有用 谢谢

3 个答案:

答案 0 :(得分:0)

发送意图

Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);

并接受其他活动

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");

希望它有效:)

答案 1 :(得分:0)

如果您想发送图片,只需发送路径。 将其改为。

bRegister.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

            Intent i = new Intent(RegisterActivity.this,MainActivity.class);
          i.putExtra("picture", picturePath);// send path
            startActivity(i);
            finish();


    }

});

并在另一项活动中接收路径。

并设为..

.setImageBitmap(BitmapFactory.decodeFile(picturePath));

2nd如果您想访问其他活动的图片

只需将共享首选项中的路径设置为USER_DP字符串即可。并随时获取它。的例如

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);

    shared.edit().putString("USER_DP",picturePath).commit();// like this

答案 2 :(得分:0)

您可以使用以下代码将路径保存到共享首选项中,您可以根据需要获取它。初始化共享偏好

SharedPreferences sharedPref = context.getSharedPreferences(SHARED,Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit();

保存路径

public void savePath(String key,String path){ editor.putString(key, path); editor.commit(); }

检索特定类中的路径 -

`public String getPath(String key){

if(sharedPref.contains(key)){

String path="";
  path=sharedPref.getString(key, " ");
 return path; 

} 返回"&#34 ;;  }`