在Android库中显示应用程序,然后在ImageView中上传所选图像

时间:2016-03-22 12:11:05

标签: android xml

有没有人知道如何在Gallery中显示我的应用程序,然后在点击时将其上传到ima​​geView中,然后我使用api上传它。但是我如何实现这一目标呢?顺便说一句,对不起,如果已经被问到我找不到一些简单易懂的东西,我刚刚开始开发Android应用程序。

如果需要更多信息,请询问

3 个答案:

答案 0 :(得分:0)

您可以创建自定义图库。 sample example of custom gallery 或参考此解决方案you have to set selected image on imageview

答案 1 :(得分:0)

public class MainActivity extends AppCompatActivity {
    private ImageButton btn;
    private ImageView imageView;
    private static int RESULT_LOAD_IMAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       btn=(ImageButton)findViewById(R.id.imageButton);
       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
               startActivityForResult(i, RESULT_LOAD_IMAGE);
           }
       });
    }
    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) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }
}

//自己管理main.xml

答案 2 :(得分:0)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">

    <ImageButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:layout_gravity="center_horizontal" />

   <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>