简体   繁体   English

从内置图库上传图像

[英]Upload Image from inbuilt Gallery

As I don't have the SD card. 由于我没有SD卡。 I want to upload image to a server from selecting image from built -in Gallery. 我想通过从内置库中选择图像来将图像上传到服务器。

Can anybody help me with this on how to place image in in-built gallery so that I can select from there. 有人可以帮助我解决如何在内置画廊中放置图片,以便从那里进行选择。

Any help will be appricated. 任何帮助都将适用。

You can go to your inbuilt gallery from the following code: 您可以从以下代码转到内置画廊:

Button btnBrowse = (Button)findViewById(R.id.btn_browse);
    btnBrowse.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
              "Select Picture"), SELECT_PICTURE);

}
});

Now onActivityResult should be like this. 现在onActivityResult应该是这样的。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode == RESULT_OK) {
     if (requestCode == SELECT_PICTURE) {
         Uri selectedImageUri = data.getData();
         selectedImagePath = getPath(selectedImageUri);
         EditText imageBrowse = (EditText)findViewById(R.id.thumb_url);
         imageBrowse.setText(selectedImagePath);
         byte[] strBytes = convertToBytes(selectedImagePath);

         imageBytes = strBytes;
     }
 }
}

ConvertToBytes method should be like this: ConvertToBytes方法应如下所示:

public byte[] convertToBytes(String selectedImagePath)
{
 try
 {
  FileInputStream fs = new FileInputStream(selectedImagePath);

  Bitmap bitmap = BitmapFactory.decodeStream(fs);

  ByteArrayOutputStream bOutput = new ByteArrayOutputStream();

  bitmap.compress(CompressFormat.JPEG,1, bOutput);

  byte[] dataImage = bOutput.toByteArray();

  return dataImage;
 }
 catch(NullPointerException ex)
 {
  ex.printStackTrace();
  return null;
 } 
 catch (FileNotFoundException e)
 {    
    e.printStackTrace();
    return null;
 }

}


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

By this you can upload the image. 这样您可以上传图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM