Metode & Algoritma | List Tutorials | Source Code | About | Sitemap
Konsultan Tesis
Bimbingan dan Konsultasi Tesis Informatika bersama team Project Graduate Indonesia. Konsultasi hanya untuk yang sudah me-Like FB kami (Silahkan LIKE tombol ini jika belum).
. Scroll kebawah untuk memasukan kode AntiSpam Protection. Hasil konsultasi akan kami kirimkan ke email Anda.

Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent




.


Metode dan Algoritma | Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent . Anda bisa melakukan konsultasi tentang Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent melalui form di samping kanan !!!

Setelah berhasil membuat Aplikasi untuk Memanggil Intent untuk Memilih (Browse File) dengan Intent. Kali ini kita pelajari cara lebih lanjut untuk memanggil Kamera Intent (Open Camera, Save Picture, Choose Photo) menggunakan Intent. Ada 4 Cara yang kita coba disini.

Program utama kita letakkan di MainActivity

MainActivity.java

package com.amijaya.androidcameraintent;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

// http://cariprogram.blogspot.com
// nuramijaya@gmail.com

public class MainActivity extends Activity {
Button Button01;
Button Button02;
Button Button03;
Button Button04;
TextView TextView01;
TextView TextView02;
ImageView ImageView01;

public static File imgfile;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


TextView01 = (TextView) findViewById(R.id.TextView01);
TextView02 = (TextView) findViewById(R.id.TextView02);
ImageView01 = (ImageView) findViewById(R.id.ImageView01);
Button01 = (Button) findViewById(R.id.Button01);

Button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});

Button02 = (Button) findViewById(R.id.Button02);

Button02.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 1);
}
});

Button03 = (Button) findViewById(R.id.Button03);

Button03.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(getTempFile(MainActivity.this)));
startActivityForResult(intent, 2);
}
});

Button04 = (Button) findViewById(R.id.Button04);

Button04.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
MainActivity.imgfile = new File(Environment
.getExternalStorageDirectory(), "tmp_"
+ String.valueOf(System.currentTimeMillis()) + ".jpg");
Uri ImageCaptureUri = Uri.fromFile(imgfile);

try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
ImageCaptureUri);
intent.putExtra("return-data", true);

startActivityForResult(intent, 3);
} catch (Exception e) {
e.printStackTrace();
}
}
});

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0: // fix
if (resultCode == RESULT_OK) {
if (data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
String nmfile = Environment.getExternalStorageDirectory()
+ "/tmp_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg";
try {
FileOutputStream out = new FileOutputStream(nmfile);
photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

photo = Bitmap.createScaledBitmap(photo, 80, 80, false);
ImageView01.setImageBitmap(photo);

TextView01.setText("Full Path = " + nmfile);

TextView02.setText("URI = "
+ Uri.fromFile(new File(nmfile)) + ", Filename = "
+ nmfile.substring(nmfile.lastIndexOf("/") + 1));
/*
try {
MediaStore.Images.Media.insertImage(getContentResolver(),
nmfile, nmfile
.substring(nmfile.lastIndexOf("/") + 1),
nmfile.substring(nmfile.lastIndexOf("/") + 1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
} else {
}
}
break;
case 1:
if (resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
String nmfile = Environment.getExternalStorageDirectory()
+ "/tmp_" + String.valueOf(System.currentTimeMillis())
+ ".jpg";
try {
FileOutputStream out = new FileOutputStream(nmfile);
photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
photo = Bitmap.createScaledBitmap(photo, 80, 80, false);
ImageView01.setImageBitmap(photo);

TextView01.setText("Full Path = " + nmfile);

TextView02.setText("URI = " + Uri.fromFile(new File(nmfile))
+ ", Filename = "
+ nmfile.substring(nmfile.lastIndexOf("/") + 1));
}
break;
case 2:
if (resultCode == RESULT_OK) {
File tmpfile = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(),
Uri.fromFile(tmpfile));
// do whatever you want with the bitmap (Resize, Rename, Add

// To Gallery, etc)
ImageView01.setImageBitmap(captureBmp);

String nmfile = Environment.getExternalStorageDirectory()
+ "/tmp_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg";
try {
FileOutputStream out = new FileOutputStream(nmfile);
captureBmp
.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

TextView01.setText("Full Path = " + nmfile);

TextView02.setText("URI = "
+ Uri.fromFile(new File(nmfile)) + ", Filename = "
+ nmfile.substring(nmfile.lastIndexOf("/") + 1));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case 3: // fix
if (resultCode == RESULT_OK) {
File resultfile = MainActivity.imgfile;
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(),
Uri.fromFile(imgfile));
// do whatever you want with the bitmap (Resize, Rename, Add
// To Gallery, etc)
ImageView01.setImageBitmap(captureBmp);

String nmfile = Environment.getExternalStorageDirectory()
+ "/tmp_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg";
try {
FileOutputStream out = new FileOutputStream(nmfile);
captureBmp
.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

TextView01.setText("Full Path = " + nmfile);

TextView02.setText("URI = "
+ Uri.fromFile(new File(nmfile)) + ", Filename = "
+ nmfile.substring(nmfile.lastIndexOf("/") + 1));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
android.database.Cursor cursor = managedQuery(contentUri, proj, // Which
// columns
// to
// return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

private File getTempFile(Context context) {
// it will return /sdcard/image.tmp
File path = new File(Environment.getExternalStorageDirectory(), context
.getPackageName());
if (!path.exists()) {
path.mkdir();
}
return new File(path, "image.tmp");
}

}

Kemudian tampilannya kita pakai saja tampilan default kemudian ditambah empat buah Button dengan nama Button01, Button02, Button03, Button04 dan dua buah TextView dengan nama TextView01, TextView02 dan satu buah ImageView dengan nama ImageView01 :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="Kamera" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Kamera" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Kamera" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Kamera" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:text="" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>

Android Manifest perlu ditambahkan permisson untuk menggunakan Camera dan permission Baca Tulis ke SDCARD.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.amijaya.androidcameraintent"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>  

Hasilnya





Project selengkapnya silakan didownload disini :

https://docs.google.com/open?id=0B4i1FYc_4RXzamxyblFvczl3SDQ

Semoga bermanfaat, Selamat Coding.


Source Code ActionScript AS3 ASP.NET AJAX C / C++ C# Clipper COBOL ColdFusion DataFlex Delphi Emacs Lisp Fortran FoxPro Java J2ME JavaScript JScript Lingo MATLAB Perl PHP PostScript Python SQL VBScript Visual Basic 6.0 Visual Basic .NET Flash MySQL Oracle Android
Related Post :


Project-G
Judul: Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent
Rating: 100% based on 99998 ratings. 5 user reviews.
Ditulis Oleh hank2

Anda sedang membaca artikel tentang Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent, Semoga artikel tentang Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent ini sangat bermanfaat bagi teman-teman semua, jangan lupa untuk mengunjungi lagi melalui link Memanggil Kamera Intent, Open Camera, Take Picture, Simpan Picture, Pilih Foto Menggunakan Intent.


Posted by: Metode Algoritma Updated at: 19.02

{ 0 komentar... Views All / Send Comment! }

Posting Komentar

Label