2013-04-25 66 views
0

我对如何获取图像名称,从相机拍摄照片(当前点击图像)或SD卡(已经粘贴或在SD卡图像)。请您帮我解决如何获取图像名称。现在如何在Android中拍摄照片或SD卡后获取图像名称

if (requestCode == CAMERA_REQUEST) { 
ImageView profImage; 
Bitmap photo = (Bitmap) data.getExtras().get("data"); 
Bitmap scaledphoto = Bitmap.createScaledBitmap(photo, height, width, 
         true); 
profImage.setImageBitmap(scaledphoto); 
//How to get name here 
} 

,这里如何得到有关数据库中保存图像的建议ImageName

或者

Imageview test=(Imageview) findViewById(R.id.testimage); 
test.setImageResource(R.drawable.androidimage); 

所以,我想获得的图像的名称,下面的图像的名字是androidimage.SO如何获得imagename。

+0

问清楚它没有帮助回答... – 2013-04-25 04:59:22

+0

PLZ澄清它更 – Dilip 2013-04-25 05:02:39

+1

看到这个http://stackoverflow.com/questions/7282426/how-to-get-image-name-using-camera-intent -in-android – 2013-04-25 05:05:40

回答

0

您可以将Bitmap传递给此方法以获取Image Uri。

public Uri getImageUri(Context inContext, Bitmap inImage) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = Images.Media.insertImage(inContext.getContentResolver(), 
       inImage, "Title", null); 
     return Uri.parse(path); 
    } 

最后,你可以设置图像图像视图与

imageView.setImageUri(mUri); 
+0

我做到了这一点,我在imageView上设置图像。如果我想知道,在imageview上设置哪个图像(图像名称)。我该怎么办?意味着如何获得图像名称。 – user2002673 2013-04-25 06:11:02

+0

试试这个String scheme = mUri.getScheme(); (scheme.equals(“file”)){ fileName = uri.getLastPathSegment(); } – Ajay 2013-04-25 06:23:44

+0

您可以在这里阅读以获得更多详细信息https://groups.google.com/forum/?fromgroups=#!topic/android-beginners/hBJxjgLBWpE – Ajay 2013-04-25 06:31:27

0

更新的代码

画廊:

Intent photoPickerIntent = new Intent(
      Intent.ACTION_GET_CONTENT); 
    photoPickerIntent.setType("image/*"); 
    startActivityForResult(photoPickerIntent, CAMERA_PIC_REQUEST); 

onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_PIC_REQUEST) { 


     Uri selectedImage = data.getData(); 

     String filePath = getRealPathFromURI(selectedImage); 
     // used in show HD images 
     BitmapFactory.Options bounds = new BitmapFactory.Options(); 
     // divide bitmap to 4 sample size it can be 2rest(2,4,8 etc) 
     bounds.inSampleSize = 4; 
     // get bitmap from bounds and file path 
     Bitmap bmp = BitmapFactory.decodeFile(filePath, bounds); 

     imageView.setImageBitmap(bmp); 
     Uri selectedImageUri1 = data.getData(); 
     String path = getRealPathFromURI(selectedImageUri1); 
     File file = new File(path); 
     textView.setText(file.getName()); 

    } 
} 

public String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 
0

请使用下面的代码。

public class UploadImageActivity extends Activity { 
private final int CAMERA_PICTURE = 1; 
private final int GALLERY_PICTURE = 2; 
private ImageView userPictureImageView; 
private Intent pictureActionIntent = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main1); 

    userPictureImageView = (ImageView) findViewById(R.id.imageView1); 
    userPictureImageView.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      startDialog(); 
     } 

    }); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == GALLERY_PICTURE) { 
     Uri uri = data.getData(); 
     if (uri != null) { 
      // User had pick an image. 
      Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null); 
      cursor.moveToFirst(); 
      // Link to the image 
      final String imageFilePath = cursor.getString(0); 
      File photos = new File(imageFilePath); 
      Bitmap b = decodeFile(photos); 
      b = Bitmap.createScaledBitmap(b, 150, 150, true); 
      userPictureImageView.setImageBitmap(b); 
      cursor.close(); 
     } 
     else { 
      Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG); 
      toast.show(); 
     } 
    } 
    else if (requestCode == CAMERA_PICTURE) { 
     if (data.getExtras() != null) { 
      // here is the image from camera 
      Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
      userPictureImageView.setImageBitmap(bitmap); 
     } 
    } 
} 

private Bitmap decodeFile(File f) { 
    try { 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale++; 
     } 

     // decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } 
    catch (FileNotFoundException e) { 
    } 
    return null; 
} 

private void startDialog() { 
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this); 
    myAlertDialog.setTitle("Upload Pictures Option"); 
    myAlertDialog.setMessage("How do you want to set your picture?"); 

    myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
      pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null); 
      pictureActionIntent.setType("image/*"); 
      pictureActionIntent.putExtra("return-data", true); 
      startActivityForResult(pictureActionIntent, GALLERY_PICTURE); 
     } 
    }); 

    myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
      pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(pictureActionIntent, CAMERA_PICTURE); 
     } 
    }); 
    myAlertDialog.show(); 
} 
} 

不要忘记在清单文件中添加相机权限。

相关问题