其實也不知道是不是通通都抓出來,不過算是把android.R.drawable的圖片都掃過了一次。
檔案在這裡,注意我設定的android:minSdkVersion="10",版本為4.0.3,如果有手機版本不符合的朋友,請在AndroidManifest.xml與project的properties中修改。
好像接下來直接放code上來感覺很混,所以我們把畫面截圖放了上來。
話說要記得,因為設計的時候沒有打算讓他跑兩次,所以按鈕也就只設計只能按一次喔!
然後,這是main.xml的布局內容:
這是Main.java 內容,我有貼心的在上面加了註解:
這是AndroidManifest.xml內容,比較需要注意權限的部分:
檔案在這裡,注意我設定的android:minSdkVersion="10",版本為4.0.3,如果有手機版本不符合的朋友,請在AndroidManifest.xml與project的properties中修改。
好像接下來直接放code上來感覺很混,所以我們把畫面截圖放了上來。
話說要記得,因為設計的時候沒有打算讓他跑兩次,所以按鈕也就只設計只能按一次喔!
![]() |
這是一開始的畫面 |
然後,這是main.xml的布局內容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="輸出前" />
<ImageView
android:id="@+id/ImageView_before_show"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@android:drawable/alert_dark_frame" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="輸出後" />
<ImageView
android:id="@+id/imageView_show"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@android:drawable/alert_dark_frame" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@android:drawable/ic_dialog_dialer" />
</LinearLayout>
這是Main.java 內容,我有貼心的在上面加了註解:
package pick.susan.idea;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
ImageButton imageButton;
ImageView imageView, imageViewBe;// 顯示從resId設定的圖片,以及轉成Bitmap之後準備要存入的圖片
TextView textView;// 進度顯示,充當log使用
final int startDrawable = android.R.drawable.alert_dark_frame;// 第一個圖案的變數
final int finalDrawable = android.R.drawable.zoom_plate;// 最後一個圖案的變數
int tihsDrawableIndex;// 要用來轉換儲存的index
Handler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tihsDrawableIndex = startDrawable;// 先把位置指向第一個index
handler = new Handler();// 配合新的線程使用的
// -----變數設定------
textView = (TextView) this.findViewById(R.id.textView_show);
imageViewBe = (ImageView) this.findViewById(R.id.ImageView_before_show);
imageView = (ImageView) this.findViewById(R.id.imageView_show);
imageButton = (ImageButton) this.findViewById(R.id.imageButton);
imageView.setImageResource(tihsDrawableIndex);
imageView.setImageResource(tihsDrawableIndex);
imageButton.setOnClickListener(mOnClickListener);
// -----變數設定------
}
private OnClickListener mOnClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
showTextThroughThread("創建文件");
final String dir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();//將圖片放在SD卡中的"Picture"資料夾
File file = new File(dir);
if (!file.exists()) {//如果資料夾不存在則建立一個資料夾
if (!file.mkdirs()) {
Log.d("Test", "創立資料夾失敗");
showTextThroughThread("創建文件失敗");
}
}
Log.d("Tes", "資料夾動作結束");
Log.d("Test", "file.exists() " + file.exists());
showTextThroughThread("資料夾動作結束");
showTextThroughThread("file.exists() " + file.exists());//看圖片資料夾有沒有存在
final ProgressDialog mDialog;//鎖定螢幕讓activity執行背景作業的dialog
mDialog = new ProgressDialog(Main.this);
mDialog.setMessage("wait...");
mDialog.show();//讓這個dialog顯示出來,注意要配合new一個Thread執行,這樣才合情合理。
mDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO 於Show()如果取消了dialog會有的動作
}
});
new Thread(new Runnable() {
@Override
public void run() {
do {
Log.d("Test", "抓取其中的id值" + tihsDrawableIndex);
showTextThroughThread("嘗試建立這個id的圖檔 :"
+ tihsDrawableIndex);
String path = dir + File.separator + "Image"
+ tihsDrawableIndex + ".png";//建立圖片的路徑
Drawable drawable = getApplicationContext()
.getResources().getDrawable(tihsDrawableIndex);
if (drawable != null
&& (drawable.getIntrinsicWidth() > 0)
&& (drawable.getIntrinsicHeight() > 0)) {// 確定這個圖案是可用的
final Bitmap bm = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
tihsDrawableIndex);// 獲取他轉乘Bitmap檔案
String mString = "圖檔資訊, 寬度: "
+ drawable.getIntrinsicWidth() + "\n高度: "
+ drawable.getIntrinsicHeight()
+ "\ndrawable.getOpacity(): "
+ drawable.getOpacity();
showTextThroughThread(mString);
if (bm != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
} catch (FileNotFoundException e1) {
Log.e("Test", "e : " + e1);
showTextThroughThread("建立FileOutputStream失敗: "
+ e1);
}
showTextThroughThread("嘗試複製檔案至SD卡");
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
try {
fos.flush();//送出
fos.close();//注意一定要關閉
showTextThroughThread("複製檔案結束");
} catch (IOException e1) {
Log.e("Test", "e : " + e1);
showTextThroughThread("複製檔案失敗: " + e1);
}
}
try {
Thread.sleep(100);// 休息一下,目的是為了讓他可以看得到運作動作
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
imageViewBe
.setImageResource(tihsDrawableIndex);// 顯示尚未轉成Bitmap的圖案
imageView.setImageBitmap(bm);// 顯示已經轉成Bitmap的圖案
tihsDrawableIndex++;//移到下一個動作
}
});
} else {
Log.e("Test", "這個id的圖不存在" + tihsDrawableIndex);
showTextThroughThread("這個id的圖不存在 :"
+ tihsDrawableIndex);
}
} while (tihsDrawableIndex <= finalDrawable);
handler.post(new Runnable() {
@Override
public void run() {
showTextThroughThread("結束");
mDialog.dismiss();
}
});
}
}).start();
}
};
//顯示文字用的,因為確定他一定會在新的thread中執行,用handler把它包起來
private void showTextThroughThread(final String content) {
if (content == null) {
textView.setText("");
} else {
handler.post(new Runnable() {
@Override
public void run() {
textView.append(content);
textView.append("\n");
}
});
}
}
}
這是AndroidManifest.xml內容,比較需要注意權限的部分:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pick.susan.idea"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
沒有留言:
張貼留言
你好,我是小書,如果文章內容有錯誤,或是看到有建議以及任何感想時,歡迎提出分享,我們一起學習一起努力。