|
Metode dan Algoritma | Browse File (Open All File & Picture) Menggunakan Intent . Anda bisa melakukan konsultasi tentang Browse File (Open All File & Picture) Menggunakan Intent melalui form di samping kanan !!!
Penulis sudah pernah menulis tentang Open File Dialog di Android. Tapi kali ini penulis merasa sangat "terpukul" ketika mengetahui cara lain yang lebih simpel :) Cara lain untuk memilih File terutama File Gambar yaitu menggunakan Intent dengan memanggil Aplikasi Android yang cocok untuk memilih file, Misalnya Gallery atau Camera. Kali ini kita pelajari cara memilih File menggunakan Intent.
Program utama kita letakkan di MainActivity
MainActivity.java
package com.amijaya.androidbrowsefilegalleryintent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
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;
TextView TextView01;
TextView TextView02;
ImageView ImageView01;
/** 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();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),0);
//startActivityForResult(intent, 1);
}
});
Button02 = (Button) findViewById(R.id.Button02);
Button02.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
}
});
Button03 = (Button) findViewById(R.id.Button03);
Button03.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
//i.setType("file/*");
startActivityForResult(i, 2);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
ImageView01.setImageURI(data.getData());
TextView01.setText("Full Path = " + getRealPathFromURI(data.getData()));
TextView02.setText("URI = " + data.getDataString() + ", Filename = " + getRealPathFromURI(data.getData()).substring(getRealPathFromURI(data.getData()).lastIndexOf("/") + 1));
}
break;
case 1:
if(resultCode == RESULT_OK){
ImageView01.setImageURI(data.getData());
TextView01.setText("Full Path = " + getRealPathFromURI(data.getData()));
TextView02.setText("URI = " + data.getDataString() + ", Filename = " + getRealPathFromURI(data.getData()).substring(getRealPathFromURI(data.getData()).lastIndexOf("/") + 1));
}
break;
case 2:
if(resultCode == RESULT_OK){
ImageView01.setImageURI(data.getData());
TextView01.setText("Full Path = " + getRealPathFromURI(data.getData()));
TextView02.setText("URI = " + data.getDataString() + ", Filename = " + getRealPathFromURI(data.getData()).substring(getRealPathFromURI(data.getData()).lastIndexOf("/") + 1));
}
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 String getRealPathFromURI(Uri contentUri) {
int columnIndex = 0;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
try {
columnIndex = cursor.getColumnIndexOrThrow
(MediaStore.Images.Media.DATA);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Exception in getRealPathFromURI",
Toast.LENGTH_SHORT).show();
finish();
return null;
}
cursor.moveToFirst();
return cursor.getString(columnIndex);
}
*/
}
Kemudian tampilannya kita pakai saja tampilan default kemudian ditambah tiga buah Button dengan nama Button01, Button02, Button03 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="Browse Image" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Browse Image" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Browse All File" android:id="@+id/Button03" 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 tidak perlu kita ubah, sesuai dengan Activity yang telah dibuat dalam hal ini MainActivity.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amijaya.androidbrowsefilegalleryintent"
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" />
</manifest>
Hasilnya
Project selengkapnya silakan didownload disini :
https://docs.google.com/open?id=0B4i1FYc_4RXzemZUbkdidjYzWlk
Semoga bermanfaat, Selamat Coding.
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 :
Judul: Browse File (Open All File & Picture) Menggunakan Intent
Rating: 100% based on 99998 ratings. 5 user reviews.
Ditulis Oleh hank2
Rating: 100% based on 99998 ratings. 5 user reviews.
Ditulis Oleh hank2
Anda sedang membaca artikel tentang
Browse File (Open All File & Picture) Menggunakan Intent, Semoga artikel tentang Browse File (Open All File & Picture) Menggunakan Intent ini sangat bermanfaat bagi teman-teman semua, jangan lupa untuk mengunjungi lagi melalui link
Browse File (Open All File & Picture) Menggunakan Intent.
{ 0 komentar... Views All / Send Comment! }
Posting Komentar