بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
گرفتن عکس از دوربین و گالری
سلام من یک ایمج وی دارم که میخوام با زدن یک دکمه یه پیغام بیاد که بگه از گالری یا از دوربین و حالا که عکس انتخاب شد بیاد داخل ایمیج وی
چکاری باید انجام بدم و اینکه وقتی از دوربین عکس گرفته میشه و وارد ایمیج وی ویشه عکس با کیفیت باشه ..
مرسی لطفا راهنمایی کنید
+1
0
لینک (9 سال پیش)
0
0
حالا از گالری بخوام چطور میشه؟؟؟ (9 سال پیش)
0
0
لینک (9 سال پیش)
0
0
لینک دادی عالیه ولی گیج کنندست . (9 سال پیش)
برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال
SaeiD-as
9 سال پیش
+1
0
سلام
من کد زیر رو برای برنامه ام نوشته ام، خیلی بد نوشته ام ولی کارم رو راه انداخته، البته فکر میکنم حتما یه کتابخونه براش وجود داشته باشه که بشه راحتتر این کار رو انجام داد:
private void selectImage() {
final CharSequence[] options = {
"" + G.context.getResources().getString(R.string.take_photo),
"" + G.context.getResources().getString(R.string.choose_photo),
"" + G.context.getResources().getString(R.string.cansel) };
AlertDialog.Builder builder = new AlertDialog.Builder(G.currentActivity);
builder.setTitle("" + G.context.getResources().getString(R.string.add_photo));
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("" + G.context.getResources().getString(R.string.take_photo))) {
capturePhoto();
} else if (options[item].equals("" + G.context.getResources().getString(R.string.choose_photo))) {
choosePhoto();
} else if (options[item].equals("" + G.context.getResources().getString(R.string.cansel))) {
dialog.dismiss();
}
}
});
builder.show();
}
public void capturePhoto() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(G.DIR_ADV + uploadImageName)));
startActivityForResult(intent, TAKE_PICTURE);
}
public void choosePhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(G.DIR_ADV + uploadImageName)));
//Log.i("LOG", "CHOOSE FILE : " + uploadImageName);
startActivityForResult(intent, CHOOSE_PICTURE);
}
final int TAKE_PICTURE = 1;
final int CHOOSE_PICTURE = 2;
private String uploadImageName = your Image Name;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Log.i("LOG", "requestCode : " + requestCode + " resultCode : " + resultCode + " data : " + data);
capturePhotoEdit = true;
mustUpload = true;
switch (requestCode) {
case TAKE_PICTURE:
//isRotate = true;
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(G.DIR_ADV + "/" + uploadImageName, options);
Log.i("LOG", "Bitmap : " + bitmap);
bitmap = Bitmap.createScaledBitmap(bitmap, G.picSize_width, G.picSize_height, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
File f = new File(G.DIR_ADV + "/" + uploadImageName);
if ( !((G.DIR_ADV + "/" + uploadImageName).isEmpty())) {
f.delete();
Log.i("LOG", "deleted Exist File");
}
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
Log.i("LOG", "created New File");
}
catch (IOException e) {
e.printStackTrace();
}
imgBuyerCaptureOK.setImageBitmap(getResizedBitmap(bitmap, rltImageBuyer.getWidth(), rltImageBuyer.getHeight()));
break;
case CHOOSE_PICTURE:
if (resultCode == RESULT_OK && data != null) {
isRotate = false;
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();
String destinationPicturePath = G.DIR_ADV + "/" + uploadImageName;
try {
BitmapFactory.Options options1 = new BitmapFactory.Options();
Bitmap bitmap1 = BitmapFactory.decodeFile(picturePath, options1);
bitmap1 = Bitmap.createScaledBitmap(bitmap1, G.picSize_width, G.picSize_height, false);
ByteArrayOutputStream bytes1 = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 50, bytes1);
copyFile(picturePath, destinationPicturePath);
File f1 = new File(G.DIR_ADV + "/" + uploadImageName);
File newFile = new File(uploadImageName);
if ( !((G.DIR_ADV + "/" + uploadImageName).isEmpty())) {
f1.delete();
Log.i("LOG", "deleted Exist File");
}
f1.createNewFile();
File photo = new File(MediaStore.Images.Media.DATA);
photo.renameTo(newFile);
FileOutputStream fo = new FileOutputStream(f1);
fo.write(bytes1.toByteArray());
fo.close();
Log.i("LOG", "created New File");
}
catch (Exception e) {
e.printStackTrace();
}
//imgBuyerCaptureOK.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imgBuyerCaptureOK.setImageBitmap(getResizedBitmap(BitmapFactory.decodeFile(destinationPicturePath), rltImageBuyer.getWidth(), rltImageBuyer.getHeight()));
//Toast.makeText(G.context, "Renamed to : " + uploadImageName, Toast.LENGTH_LONG).show();
}
break;
}
}
public static void copyFile(String sourceFile, String destinationFile) {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile);
byte[] buffer = new byte[G.DOWNLOAD_BUFFER_SIZE];
int len = 0;
Log.i("LOG", "outputStream before : " + len);
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
Log.i("LOG", "outputStream after : " + len);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
private boolean isRotate = false;
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
if (isRotate) {
float angle = 90;
matrix.postRotate(angle);
}
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
//matrix.setScale(50, 50);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
//bm.recycle();
return resizedBitmap;
}
فقط کافیه که تابع selectImage() رو روی باتنی که قراره انجام بدی ست کنی.
البته اینم بگم که این کد رو خیلی بد نوشته ام، خیلی وقتا زمانی که روی انتخاب عکس از گالری انتخاب می کنم برنامه ام کرش میکنه و هنوز دلیلش رو متوجه نشده ام!!
اگه شما فهمیدید به منم بگید :)
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .