2013-01-21 144 views
8

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

感谢

+0

如果我的回答可以帮助您,然后接受的答案 – Janmejoy

回答

7

这里有以下链接..

create image button

上传图片

example 1

example 2

example 3

+0

感谢您的链接。我设法在您提供的示例中运行代码。如果应用程序要求用户注册帐户并且用户必须将图像作为个人资料图片插入,那么一旦用户登录到他的帐户,用户就可以看到个人资料图片。那么如何做到这一点?类似像whatsapp;他们如何存储用户上传到配置文件的图片? – Mack

+0

@Mack你的意思是上传图片,它可以用几种方式完成,比如从Facebook或Twitter解析或从图库中解析 – Janmejoy

+0

我有一个应用程序要求用户为该个人资料插入图片。我的问题是:我需要在哪里存储用户图片? MySQL DB,Sqlite DB,内部存储或外部存储? – Mack

0

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

} 
相关问题