从Gallery android应用程序上传图像

时间:2012-03-01 16:05:55

标签: java android xml

欢迎大家, 我的应用程序需要从库中检索任何图片..我在谷歌搜索 然后我发现了一个简单的代码如下所示+我的代码,我在我的应用程序中使用它可以工作(当我按下浏览按钮时它会进入图库)但我不知道它是否检索图片 因为你知道模拟器没有任何图像我试图添加图片 模拟器(通过使用DDMS),但我不能,所以我不知道这段代码中的这些函数(onActivityResult()和String getPath())是否检索所选图片?

Q1:如果是,请告诉我如何将其添加到Case对象(它是我项目中的一个类) ?

public class MyCase {


    //private ?? pic;
    private String name;
    private String gender ;
    private int age;
    private String clothes;
    private String MoreInfo;
    private String time;
    private String location;
    private  int ID;
}

Q2:案例图片的数据类型应该使用什么?

非常感谢,

public class CreateNewForm extends Activity implements OnItemSelectedListener  {


        Button Browse, Cancelb, Nextb;
        ImageView  CasePic;
        Spinner CaseDurationH, CaseDurationM;
        TextView tesst;
        RadioGroup GenderSelection;
        EditText CaseName, CaseClothes, CaseMoreInfo, CaseAge;

        //For Browsering Picture 
        private static final int SELECT_PICTURE = 1;
        private String selectedImagePath;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {

            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_new_form);

            //To Call initializer Function 
            initializer();

            // 1-For Uploading Picture
            Browse.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });


    }

    // To initialize the variables 
    private void initializer() {
        // TODO Auto-generated method stub
        //This information will be filled by a plaintiff
                //CasePic = (ImageView) findViewById(R.id.imageView1);
                CaseName= (EditText) findViewById(R.id.caseNm);
                GenderSelection= (RadioGroup) findViewById(R.id.radioGroup1);
                CaseAge= (EditText) findViewById(R.id.caseaage);
                tesst= (TextView) findViewById(R.id.textView8);
                CaseDurationH= (Spinner) findViewById(R.id.Shr);
                CaseDurationM= (Spinner) findViewById(R.id.Smin);
                CaseClothes= (EditText) findViewById(R.id.caseClothes);
                CaseMoreInfo= (EditText) findViewById(R.id.caseMrInfo);
                CasePic = (ImageView) findViewById(R.id.casepic);
                Browse = (Button) findViewById(R.id.browseCasePic);
                Nextb= (Button)findViewById(R.id.next1);
                Cancelb = (Button) findViewById(R.id.cancel1);
    }

        //For Uploading Picture
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                }
            }
        }

        //For Uploading Picture
        public String 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();
            return cursor.getString(column_index);
        }

1 个答案:

答案 0 :(得分:0)

您可以使用DDMS FileExplorer或“adb push”将图像推送到SD卡上。完成后,您需要运行Media Scanner。 Media Scanner将扫描SDCard并将找到的图像添加到MediaStore。您可以以编程方式触发Media Scanner,也可以使用模拟器上的内置Dev Tools应用程序。

以编程方式触发Media Scanner的代码:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));