2011-11-03 36 views
7

Được rồi Tôi đang tạo ứng dụng Android và sử dụng CAPTURE_PIC_REQUEST để chụp ảnh và lưu trữ ảnh đó dưới dạng bản xem trước trên màn hình trong trình xem hình ảnh. Tôi muốn biết làm thế nào để có được Uri và thực sự con đường bởi vì tôi đang lên kế hoạch lấy thông tin đó, lưu nó và truy cập nó sau này. Vì vậy, tôi không thể tìm cách để có được tên Uri hoặc đường dẫn từ bức ảnh mà tôi vừa chụp. Đây là đoạn mã từ hoạt động của tôi mà là xử lýNhận đường dẫn tệp từ ảnh trong ImageView

package com.CS480; 

import java.io.File; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class AddEx extends Activity { 
    static final int CAMERA_PIC_REQUEST = 1337; 
    public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add); 

    Button camera = (Button) findViewById(R.id.picButton); 
    camera.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);    
     }    
    }); 

} 
@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) { 
     Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
     ImageView image = (ImageView) findViewById(R.id.returnedPic); 
     image.setImageBitmap(thumbnail); 
    } 
} 

} 

Vậy làm thế nào từ hình ảnh đó mà tôi đang nhận được trở lại trên ứng dụng để tôi có được vị trí tập tin

Trả lời

8

Theo như tôi biết, bạn có thể làm một cái gì đó như thế này:

private Uri mImageCaptureUri; 

public class AddEx extends Activity { 
    static final int CAMERA_PIC_REQUEST = 1337; 
    public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add); 

    Button camera = (Button) findViewById(R.id.picButton); 
    camera.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent cameraIntent = 
        new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
        mImageCaptureUri); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);    
     }    
    }); 

} 
@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) { 
     Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
     ImageView image = (ImageView) findViewById(R.id.returnedPic); 
     image.setImageBitmap(thumbnail); 

     String pathToImage = mImageCaptureUri.getPath(); 

     // pathToImage is a path you need. 

     // If image file is not in there, 
     // you can save it yourself manually with this code: 
     File file = new File(mImageCaptureUri.getPath()); 
     FileOutputStream fOut = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want 
    } 
}  

Từ tài liệu Android về android.provider. MediaStore.EXTRA_OUTPUT:

Tên của Intent-extra được sử dụng để biểu thị trình giải quyết nội dung Uri được sử dụng để lưu trữ hình ảnh hoặc video được yêu cầu.

1

Bạn có thể thử nhận được URI của hình ảnh với

data.getExtras.get("URI"); 

nhưng điều này có thể không hoạt động. Những gì bạn có thể làm chỉ đơn giản là lưu bản thân Bitmap. Xem câu hỏi này để biết hướng dẫn về cách thực hiện: Save bitmap to location

1

Khi bạn chụp ảnh, bạn phải lưu trữ ở đâu đó để lấy vị trí của hình ảnh.

Để làm điều này, bạn có thể lưu trữ đến MediaStore:

String result = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", ""); 

imageFileUri = Uri.parse(result); 
0

Hãy thử get này tập tin ảnh dạng đường Bitmap

public void uploadBitmap(Context mContext, Bitmap bitmap) { 

    String imagePath = null; 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    Uri uri; 
    Cursor cursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, MediaStore.Images.Media.DATE_ADDED, null, "date_added DESC"); 
    if (cursor != null && cursor.moveToFirst()) { 
     do { 
      uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))); 
      imagePath = uri.toString(); 
      Log.d("pathatka", uri.toString()); 
      break; 
     } while (cursor.moveToNext()); 
     cursor.close(); 
    } 

} 
Các vấn đề liên quan