Tôi có một hoạt động trong đó tôi cung cấp cho người dùng tùy chọn nhấp vào hình ảnh từ máy ảnh, sau đó tôi lưu trữ hình ảnh này trong một mảng byte và trong Cơ sở dữ liệu. Tuy nhiên mã của tôi dường như không làm việc trên Samsung Galaxy S3 dưới đây là các mã:Mục đích của máy ảnh không hoạt động với Samsung Galaxy S3
Máy ảnh kêu gọi ý:
if (i == 0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
On phương pháp Hoạt động cho máy ảnh:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1337 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
thumbnail = (Bitmap) extras.get("data");
image(thumbnail);
} else {
Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Hình ảnh của tôi (Bitmap thumbnail) function:
public void image(Bitmap thumbnail) {
Bitmap photo = thumbnail;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
b = bos.toByteArray();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
Bitmap bt = Bitmap.createScaledBitmap(photo, 100, 80, false);
imageview.setImageBitmap(bt);
}
Tuy nhiên điều này không hoạt động với Samsung S3, tôi đã thay đổi c ode sau đây và bây giờ nó hoạt động với Samsung S3, tuy nhiên nó không hoạt động với bất kỳ thiết bị nào khác.
Máy ảnh Ý định:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Hoạt động cho kết quả:
startActivityForResult(intent, CAMERA_IMAGE_CAPTURE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
// Describe the columns you'd like to have returned. Selecting from the Thumbnails location gives you both the Thumbnail Image ID, as well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
Cursor myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try{
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}
finally{myCursor.close();}
//Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";
try{
myCursor.moveToFirst();
//This will actually give yo uthe file path location of the image.
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
}
finally{myCursor.close();}
// These are the two URI's you'll be interested in. They give you a handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
// I've left out the remaining code, as all I do is assign the URI's to my own objects anyways...
// Toast.makeText(this, ""+largeImagePath, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show();
if (largeImagePath != null) {
// Toast.makeText(this, "" + largeImagePath, Toast.LENGTH_LONG).show();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
// thumbnail = (BitmapFactory.decodeFile(picturePath));
thumbnail = BitmapFactory.decodeFile((largeImagePath), opts);
System.gc();
if (thumbnail != null) {
Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();
}
image(thumbnail);
}
if (uriLargeImage != null) {
Toast.makeText(this, "" + uriLargeImage, Toast.LENGTH_LONG).show();
}
if (uriThumbnailImage != null) {
Toast.makeText(this, "" + uriThumbnailImage, Toast.LENGTH_LONG).show();
}
}
Đây là hình ảnh của tôi() chức năng:
public void image(Bitmap thumbnail) {
b = null;
Bitmap photo = thumbnail;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
b = bos.toByteArray();
if (b != null) {
Toast.makeText(this, "Success Yeah" + b, Toast.LENGTH_LONG).show();
}
}
Trong khi như tất cả các ba 1) uriLargeImage 2) largeImagePath 3) uriThumbnailImage trả lại cho tôi đường dẫn hoặc URI Tôi không thể thiết lập bitmap đã tạo cho ImageView của mình. Tuy nhiên đây là trường hợp chỉ với Samsung S3, nếu tôi chạy đoạn mã đã chỉnh sửa ở trên với bất kỳ thiết bị nào khác, chương trình sẽ bị treo.
Trong manifest Tôi đã sử dụng
android:configChanges="keyboardHidden|orientation"
Dựa trên hướng dẫn: http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
Tuy nhiên nếu tôi chụp ảnh trong chế độ phong cảnh, mọi thứ hoạt động tốt! Tôi bị bối rối!! (Trong Samsung S3)
tôi đã gặp phải sự cố tương tự với thiết bị cụ thể này. tôi đã giải quyết vấn đề này sau nhiều thử nghiệm và lỗi metho. bạn có thể có ý tưởng tốt hơn về vấn đề này từ [ở đây] (http://stackoverflow.com/questions/14495304/camera-force-closing-issue-in-samsung-galaxy-s3-version-4-1-1/14640678 # 14640678) –
Các tham chiếu trên là cùng một người bạn đời, tôi đã tham khảo từ cùng một hướng dẫn, trong khi mọi thứ dường như làm việc tốt vấn đề duy nhất là tôi mất dữ liệu ở đâu đó không nên xảy ra với khai báo kê khai. – Skynet
bạn có phiền khi liên kết dự án của mình không? Tôi sẽ không bận tâm kiểm tra một số công cụ. –