【问题标题】:How can I select an image to use as a 'profile picture'?如何选择要用作“个人资料图片”的图像?
【发布时间】:2014-02-11 20:14:53
【问题描述】:

您好,我有一个时间表应用程序,它有一个主菜单。我想要一个按钮,上面写着“选择图像”或类似的东西,所以当用户点击它时,他会进入他的画廊并选择照片,然后它会保存在我创建的时间表应用程序中。我希望有人可以帮助我,因为我在互联网上找到的唯一信息是我应该使用 android.intent.action.PICK?提前致谢

更新: 我这样做了,然后现在单击按钮加载图库(效果很好)。一旦我选择了一张图片,它就会显示“不幸的是,时间表已停止”。怎么了。这是代码。

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Main extends Activity {

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


        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });



        //Button Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);

        //Setting up the button references
        Button week1 = (Button) findViewById(R.id.week1button);
        Button week2 = (Button) findViewById(R.id.week2button);

        week1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.timetable.WEEK1"));
            }
        });

                week2.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        buttonSound.start();
                        startActivity(new Intent("com.example.timetable.WEEK2"));
                    }
        });       

          };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




private static int RESULT_LOAD_IMAGE = 1;

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

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

【问题讨论】:

  • Google "android.intent.action.PICK" 搜索它的用途以及是否适合您。不?搜索“pick image intent android”你会得到这个stackoverflow.com/questions/2507898/…也许这可以帮助你。
  • 您能看一下我更改后的原始帖子吗?它似乎无法正常工作?!?

标签: android eclipse android-intent action


【解决方案1】:

假设它是一个社交应用程序,那么我建议使用this 教程或this SO question 来导入图片,然后将其保存在远程数据库中。

【讨论】:

  • 好的,谢谢你的链接。我自己找不到太多。我快速浏览了一下,看起来像是我需要的,所以明天试试,如果它有效,我会告诉你。谢谢
  • 您能看一下我更改后的原始帖子吗?它似乎无法正常工作?!?
猜你喜欢
  • 2017-09-30
  • 2011-09-09
  • 2020-12-17
  • 1970-01-01
  • 2013-12-09
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多