Pada kali ini, akan membahas cara untuk menginput camera dan menyimpan ke dalam memory Handphone :
buatlah class java dengan nama FotoLain.java
//awal
public class FotoLain extends AppCompatActivity{
private Button btn,save;
private ImageView imageview;
private static final String IMAGE_DIRECTORY = "/pengemudi_upload_camera";
private final int CAMERA = 1;
Intent intent;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foto_lain);
imageView = (ImageView) findViewById(R.id.imageView);
btn = (Button) findViewById(R.id.btn);
save = (Button) findViewById(R.id.save);
btn.setText("Ambil Foto Bukti Lain-lain");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
//untuk mengambil foto agar kualitas baik
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//intent = new Intent(android.provider.MediaStore.EXTRA_OUTPUT);
startActivityForResult(intent, CAMERA);
/*Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);*/
}
});
}
@RequiresApi(api = Build.VERSION_CODES.FROYO)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == CAMERA ) {
if (resultCode == RESULT_OK) {
try {
Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imageView.setImageBitmap(thumbnail);
//Bitmap imgView.setImageBitmap(thumbnail);
//imageurl = getRealPathFromURI(imageUri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void onTaskCompleted(String response, int serviceCode) {
AndyUtils.removeSimpleProgressDialog();
Log.d("res", response.toString());
switch (serviceCode) {
case CAMERA:
if (parseContent.isSuccess(response)) {
String url = parseContent.getURL(response);
//aQuery.id(imageview).image(url);
// String komentar= isi_komentar;
}
}
}
@RequiresApi(api = Build.VERSION_CODES.FROYO)
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
imageView.setImageURI(Uri.parse(f.getPath()));
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
}
//akhir
kemudian layout activity_foto_lain.xml
//awal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Capture Image and upload to server" />
<ImageView
android:id="@+id/imageView"
android:layout_width="115dp"
android:layout_height="115dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Simpan" />
</LinearLayout>
//akhir
Demikian semoga dapat menjadi referensi Terima kasih
buatlah class java dengan nama FotoLain.java
//awal
public class FotoLain extends AppCompatActivity{
private Button btn,save;
private ImageView imageview;
private static final String IMAGE_DIRECTORY = "/pengemudi_upload_camera";
private final int CAMERA = 1;
Intent intent;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foto_lain);
imageView = (ImageView) findViewById(R.id.imageView);
btn = (Button) findViewById(R.id.btn);
save = (Button) findViewById(R.id.save);
btn.setText("Ambil Foto Bukti Lain-lain");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
//untuk mengambil foto agar kualitas baik
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//intent = new Intent(android.provider.MediaStore.EXTRA_OUTPUT);
startActivityForResult(intent, CAMERA);
/*Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);*/
}
});
}
@RequiresApi(api = Build.VERSION_CODES.FROYO)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == CAMERA ) {
if (resultCode == RESULT_OK) {
try {
Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);
imageView.setImageBitmap(thumbnail);
//Bitmap imgView.setImageBitmap(thumbnail);
//imageurl = getRealPathFromURI(imageUri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void onTaskCompleted(String response, int serviceCode) {
AndyUtils.removeSimpleProgressDialog();
Log.d("res", response.toString());
switch (serviceCode) {
case CAMERA:
if (parseContent.isSuccess(response)) {
String url = parseContent.getURL(response);
//aQuery.id(imageview).image(url);
// String komentar= isi_komentar;
}
}
}
@RequiresApi(api = Build.VERSION_CODES.FROYO)
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
imageView.setImageURI(Uri.parse(f.getPath()));
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
}
//akhir
kemudian layout activity_foto_lain.xml
//awal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Capture Image and upload to server" />
<ImageView
android:id="@+id/imageView"
android:layout_width="115dp"
android:layout_height="115dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Simpan" />
</LinearLayout>
//akhir
Demikian semoga dapat menjadi referensi Terima kasih
Comments
Post a Comment