請看下列範例:
Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(photo.getLocalPath()); intent.setDataAndType(Uri.fromFile(file), "image/*"); context.startActivity(intent);
真的很簡單!
Android是Google為手機所打造的作業系統,良好的介面和易於學習的語言。這裡記錄著我的學習片段,就像一杯杯的咖啡,香韻留存。
Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(photo.getLocalPath()); intent.setDataAndType(Uri.fromFile(file), "image/*"); context.startActivity(intent);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); startActivityForResult(intent, SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
if (resultCode == RESULT_OK) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(data.getData(), null, null, null, null);
if (cursor.moveToFirst()) {
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String absoluteFilePath = cursor.getString(idx);
if (absoluteFilePath != null)
photoPath = absoluteFilePath;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1234567; ... mBtnTakePicture.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { //define the file-name to save photo taken by Camera activity String fileName = "new-photo-name.jpg"; //create parameters for Intent with filename ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera"); //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState) imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); //create new Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { //use imageUri here to access the image Cursor cursor = null; try { String [] proj={ MediaStore.Images.Media.DATA }; //, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION }; cursor = RecordView.this.managedQuery(imageUri, 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 file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); //int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION); if (cursor.moveToFirst()) { String imagePath = cursor.getString(file_ColumnIndex); .... } finally { if (cursor != null) { cursor.close(); } }
BitmapFactory.Options option1s = new BitmapFactory.Options(); option1s.inJustDecodeBounds = true; BitmapFactory.decodeFile(mediaPath, option1s); int oriheight = option1s.outHeight; int oriwidth = option1s.outWidth; double widthScale = (double)oriwidth / (double)(drawRect.right-drawRect.left+1); double heightScale = (double)oriheight / (double)(drawRect.bottom - drawRect.top+1); int scale = 1; if (widthScale > heightScale) scale = (int)heightScale; else scale = (int)widthScale; if (scale < 1) scale = 1; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = scale; Bitmap bmp = BitmapFactory.decodeFile(mediaPath, options);
int newWidth = (drawRect.right-drawRect.left); int newHeight = (drawRect.bottom-drawRect.top); if (bmp.getWidth() < newWidth) newWidth = bmp.getWidth(); if (bmp.getHeight() < newHeight) newHeight = bmp.getHeight(); Bitmap resizeBmp = Bitmap.createBitmap(bmp, (bmp.getWidth()/2)-(newWidth/2), (bmp.getHeight()/2)-(newHeight/2), newWidth, newHeight);
if (widthScale > heightScale) { scale = 2 ^ ((int) Math.ceil(Math.log((drawRect.bottom-drawRect.top+1) / (double) oriheight) / Math.log(0.5)) -2); } else { scale = 2 ^ ((int) Math.ceil(Math.log((drawRect.right-drawRect.left+1) / (double) oriwidth) / Math.log(0.5)) -2); }
Float dbear = new Float(this.myloc.bearingTo(oxlocate)); if (dbear.isNaN() == false) {
<?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" android:background="#a4c639"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/awesomepager"/> </LinearLayout>程式中要使用ViewPager來顯示資訊必須自己繼承PagerAdapter,來完成手指滑動切換視窗的動作。
package com.geekyouup.paug.awesomepager; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.TextView; public class AwesomePagerActivity extends Activity { private ViewPager awesomePager; private static int NUM_AWESOME_VIEWS = 20; private Context cxt; private AwesomePagerAdapter awesomeAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cxt = this; awesomeAdapter = new AwesomePagerAdapter(); awesomePager = (ViewPager) findViewById(R.id.awesomepager); awesomePager.setAdapter(awesomeAdapter); } private class AwesomePagerAdapter extends PagerAdapter{ @Override public int getCount() { return NUM_AWESOME_VIEWS; } /** * Create the page for the given position. The adapter is responsible * for adding the view to the container given here, although it only * must ensure this is done by the time it returns from * {@link #finishUpdate()}. * * @param container The containing View in which the page will be shown. * @param position The page position to be instantiated. * @return Returns an Object representing the new page. This does not * need to be a View, but can be some other container of the page. */ @Override public Object instantiateItem(View collection, int position) { TextView tv = new TextView(cxt); tv.setText("Bonjour PAUG " + position); tv.setTextColor(Color.WHITE); tv.setTextSize(30); ((ViewPager) collection).addView(tv,0); return tv; } /** * Remove a page for the given position. The adapter is responsible * for removing the view from its container, although it only must ensure * this is done by the time it returns from {@link #finishUpdate()}. * * @param container The containing View from which the page will be removed. * @param position The page position to be removed. * @param object The same object that was returned by * {@link #instantiateItem(View, int)}. */ @Override public void destroyItem(View collection, int position, Object view) { ((ViewPager) collection).removeView((TextView) view); } @Override public boolean isViewFromObject(View view, Object object) { return view==((TextView)object); } /** * Called when the a change in the shown pages has been completed. At this * point you must ensure that all of the pages have actually been added or * removed from the container as appropriate. * @param container The containing View which is displaying this adapter's * page views. */ @Override public void finishUpdate(View arg0) {} @Override public void restoreState(Parcelable arg0, ClassLoader arg1) {} @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) {} } }在範例中,可以看到需要告訴程式有多少頁面可以滑動(NUM_AWESOME_VIEWS),然後當滑動到該頁面是要如何顯示(instantiateItem)。其實這還蠻簡單的,請自行參考上述的範例。 到這裡可能會覺得好像還少了一點東西!對,想Google+上面不是還會顯示目前頁面的indicator嗎?該如何達成?賣個關子,下次說給你聽。呵呵! PS, 以上的範例是參考AwesomePager,可以點擊到該項目的網頁下載程式與說明。
ByteArrayOutputStream out = new ByteArrayOutputStream(); thumb.compress(Bitmap.CompressFormat.PNG, 100, out); values.put(FIELD_THUMB, out.toByteArray());
byte[] buf; buf = cur.getBlob(FIELD_THUMB_IDX); Bitmap bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length);