2014-01-10 212 views
0

我有一个n应用程序要求用户从图库中选择图像,它将以图像按钮显示。我怎样才能得到一个imagebutton中的图像的路径作为一个字符串?获取图像的路径

请帮助

public class NewPortal extends Activity implements OnClickListener, android.view.View.OnClickListener 

{ 
    Bitmap bmp; 
public static final int REQUEST_CODE =1; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 

setContentView(R.layout.portallayout); 
ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton2); 
    btn1.setOnClickListener(this); 

    Button btn2 = (Button)findViewById(R.id.button2); 
    btn2.setOnClickListener(this); 
} 

public void onClick(View v) { 

Toast pieceToast=null; 

EditText eiteText; 
switch (v.getId()) { 

    case R.id.imageButton2: 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, REQUEST_CODE); 
    int RESULT_LOAD_IMAGE = 1; 
    break; 

    case R.id.button2: 

    eiteText=(EditText)findViewById(R.id.editText2); 
    String result=eiteText.getText().toString(); 

    MySQLiteEntity entity = new MySQLiteEntity(1, 
      "Random Post title", 
      "Image", 
      path of image , 
      result); 

    MYSQLiteDataSource datasource = new MYSQLiteDataSource(getApplicationContext()); 
    datasource.createPost(entity); 

    break; 
} 
} 



    @Override 
public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 

} 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult1(requestCode, resultCode, data); 

    if(requestCode == RESULT_CANCELED) return; 

    ParcelFileDescriptor fd; 
    try { 
     fd = getContentResolver().openFileDescriptor(data.getData(), "r"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     return; 
    } 

    Bitmap bmp = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor()); 
    ImageButton imgButton = (ImageButton)findViewById(R.id.imageButton2); 
    imgButton.setImageBitmap(bmp); 
} 
    } 

回答

1

使用此代码从图库中选择图片:

public void photo(View v) { 
    Intent i = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    RESULT_LOAD_IMAGE = 1; 
    startActivityForResult(i, RESULT_LOAD_IMAGE); 
} 

并为获取路径,你可以使用:

@Override 
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(); 

     } 
} 

其中RESULT_LOAD_IMAGE是整数声明全球0!

然后尝试下面的代码,将存储在SD卡中的文件的位图图像设置为ImageView。

File imgFile = new File(“/sdcard/Images/test_image.jpg”); 
if(imgFile.exists()){ 

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 
myImage.setImageBitmap(myBitmap); 

}