【问题标题】:allow the user to insert an image in android app允许用户在 android 应用程序中插入图像
【发布时间】:2013-01-21 05:57:29
【问题描述】:

我的问题是:如何创建允许用户从手机上传图像并将其作为图片配置文件插入应用程序的 imageButton?例如whatsapp,它允许用户从手机中选择图像并将其设置为图片配置文件。

谢谢

【问题讨论】:

  • 如果我的回答对您有帮助,请接受回答

标签: android android-widget android-imageview android-image


【解决方案1】:

这里有以下链接..

create image button

上传图片

example 1

example 2

example 3

【讨论】:

  • 感谢您的链接。我设法运行了您提供的示例中的代码。如果应用程序要求用户注册一个帐户并且用户必须插入图像作为个人资料图片,那么一旦用户登录到他的帐户,用户就可以看到个人资料图片。那怎么做呢?类似whatsapp;他们如何存储用户上传到个人资料的图片?
  • @Mack 你的意思是上传图片可以通过多种方式完成,比如从 facebook 或 twitter 或从图库中解析
  • 我有一个应用程序要求用户为个人资料插入图片。我的问题是:我需要在哪里存储用户图片? MySQL DB、Sqlite DB、内部存储还是外部存储?
【解决方案2】:

我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

<ImageView
    android:id="@android:id/icon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:onClick="selectImage"
    />

我的文件

public class Test extends AppCompatActivity {
private static final int SELECT_PICTURE = 0;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    imageView = (ImageView) findViewById(android.R.id.icon);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bitmap bitmap = getPath(data.getData());
        imageView.setImageBitmap(bitmap);
    }
}

private Bitmap 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();
    String filePath = cursor.getString(column_index);
   // cursor.close();
    // Convert file path into bitmap image using below line.
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);

    return bitmap;
}

private void selectImage() {

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

}

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多