Skip to content
This repository was archived by the owner on Nov 28, 2021. It is now read-only.

Commit 236cdb6

Browse files
committed
Android Studio格式化
优化代码,修复错误
1 parent c4c007d commit 236cdb6

File tree

10 files changed

+151
-189
lines changed

10 files changed

+151
-189
lines changed

.idea/runConfigurations.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ to attach them to the start of each source file to most effectively
631631
state the exclusion of warranty; and each file should have at least
632632
the "copyright" line and a pointer to where the full notice is found.
633633

634-
Selecting pictures instead of taking photoes.
634+
Selecting media instead of taking photos or records.
635635
Copyright (C) 2017 XFY9326
636636

637637
This program is free software: you can redistribute it and/or modify

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 21
5-
buildToolsVersion "21.1.0"
5+
buildToolsVersion '25.0.3'
66

77
defaultConfig {
88
applicationId "tool.xfy9326.selectmedia"
9-
minSdkVersion 10
9+
minSdkVersion 14
10+
//noinspection OldTargetApi
1011
targetSdkVersion 21
1112
versionCode 1
1213
versionName "1.0"

app/src/main/AndroidManifest.xml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="tool.xfy9326.selectmedia" >
4-
5-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7-
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="tool.xfy9326.selectmedia">
5+
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
8+
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
89

910
<application
11+
android:icon="@mipmap/ic_launcher"
1012
android:label="@string/app_name"
11-
android:icon="@mipmap/ic_launcher">
12-
13+
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
14+
1315
<activity
1416
android:name=".MainActivity"
17+
android:excludeFromRecents="true"
1518
android:label="@string/app_name"
16-
android:excludeFromRecents="true"
17-
android:launchMode="singleTask"
18-
android:taskAffinity=""
19-
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar">
19+
android:launchMode="singleTask"
20+
android:taskAffinity=""
21+
android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar">
2022
<intent-filter>
21-
<action android:name="android.media.action.IMAGE_CAPTURE" />
22-
<action android:name="android.media.action.VIDEO_CAPTURE" />
23-
<category android:name="android.intent.category.DEFAULT" />
23+
<action android:name="android.media.action.IMAGE_CAPTURE" />
24+
<action android:name="android.media.action.VIDEO_CAPTURE" />
25+
<category android:name="android.intent.category.DEFAULT" />
2426
</intent-filter>
2527
</activity>
26-
28+
2729
</application>
2830

2931
</manifest>

app/src/main/java/tool/xfy9326/selectmedia/MainActivity.java

Lines changed: 85 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,110 @@
22

33
import android.app.Activity;
44
import android.app.AlertDialog;
5+
import android.content.DialogInterface;
56
import android.content.Intent;
67
import android.content.res.AssetFileDescriptor;
78
import android.net.Uri;
89
import android.os.Bundle;
9-
import android.os.Handler;
10-
import android.os.Message;
1110
import android.provider.MediaStore;
11+
1212
import java.io.File;
1313
import java.io.FileInputStream;
1414
import java.io.FileOutputStream;
1515

1616
//Made By XFY9326
1717
//2017-10-12
1818

19-
public class MainActivity extends Activity
20-
{
21-
private static final int RESULT_CODE = 1;
22-
private static final int MSG = 2;
23-
24-
//false:send uri true:copy file
25-
private boolean file_mode = false;
26-
private File file;
27-
private AlertDialog load;
28-
private Intent asset_intent;
29-
private Handler process = new Handler(){
30-
31-
@Override
32-
public void handleMessage(Message msg)
33-
{
34-
if (msg.what == MSG)
35-
{
36-
load.cancel();
37-
setResult(RESULT_OK, asset_intent);
38-
finish();
39-
System.gc();
40-
41-
}
42-
super.handleMessage(msg);
43-
}
44-
45-
};
19+
public class MainActivity extends Activity {
20+
private static final int RESULT_CODE = 1;
21+
//false:send uri true:copy file
22+
private boolean file_mode = false;
23+
private File file;
24+
private AlertDialog load;
25+
private Intent asset_intent;
4626

4727
@Override
48-
protected void onCreate(Bundle savedInstanceState)
49-
{
28+
protected void onCreate(Bundle savedInstanceState) {
5029
super.onCreate(savedInstanceState);
51-
startSelect();
30+
startSelect();
5231
}
5332

54-
@Override
55-
protected void onActivityResult(int requestCode, int resultCode, Intent data)
56-
{
57-
if (requestCode == RESULT_CODE)
58-
{
59-
if (data != null)
60-
{
61-
asset_intent = data;
62-
if (file_mode)
63-
{
64-
loading();
65-
saveMediaToFile();
66-
}
67-
else
68-
{
69-
setResult(RESULT_OK, data);
70-
finish();
71-
System.gc();
72-
}
73-
}
74-
}
75-
super.onActivityResult(requestCode, resultCode, data);
76-
}
33+
@Override
34+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
35+
if (requestCode == RESULT_CODE) {
36+
if (data != null) {
37+
asset_intent = data;
38+
if (file_mode) {
39+
loading();
40+
saveMediaToFile();
41+
} else {
42+
setResult(RESULT_OK, data);
43+
finish();
44+
System.gc();
45+
}
46+
}
47+
}
48+
super.onActivityResult(requestCode, resultCode, data);
49+
}
7750

78-
//Open picture or video selector
79-
private void startSelect()
80-
{
81-
Intent base_intent = getIntent();
82-
Intent new_intent = new Intent();
83-
Uri uri = base_intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
84-
if (uri != null)
85-
{
86-
file_mode = true;
87-
file = new File(UriMethod.getUriAbsolutePath(this, uri));
88-
}
89-
if (base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE) || base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE_SECURE))
90-
{
91-
new_intent.setType("image/*");
92-
}
93-
else if (base_intent.getAction().equals(MediaStore.ACTION_VIDEO_CAPTURE))
94-
{
95-
new_intent.setType("video/*");
96-
}
97-
new_intent.setAction(Intent.ACTION_GET_CONTENT);
98-
startActivityForResult(new_intent, RESULT_CODE);
99-
}
51+
//Open picture or video selector
52+
private void startSelect() {
53+
Intent base_intent = getIntent();
54+
Intent new_intent = new Intent();
55+
Uri uri = base_intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
56+
if (uri != null) {
57+
file_mode = true;
58+
file = new File(UriMethod.getUriAbsolutePath(this, uri));
59+
}
60+
if (base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE) || base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)) {
61+
new_intent.setType("image/*");
62+
} else if (base_intent.getAction().equals(MediaStore.ACTION_VIDEO_CAPTURE)) {
63+
new_intent.setType("video/*");
64+
}
65+
new_intent.setAction(Intent.ACTION_GET_CONTENT);
66+
startActivityForResult(new_intent, RESULT_CODE);
67+
}
10068

101-
private void loading()
102-
{
103-
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
104-
dialog.setTitle(R.string.loading);
105-
dialog.setMessage(getString(R.string.loading_msg) + file.getAbsolutePath().toString());
106-
dialog.setCancelable(false);
107-
load = dialog.show();
108-
}
69+
private void loading() {
70+
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
71+
dialog.setTitle(R.string.loading);
72+
dialog.setMessage(getString(R.string.loading_msg) + file.getAbsolutePath());
73+
dialog.setCancelable(false);
74+
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
75+
@Override
76+
public void onCancel(DialogInterface dialog) {
77+
setResult(RESULT_OK, asset_intent);
78+
finish();
79+
System.gc();
80+
}
81+
});
82+
load = dialog.show();
83+
}
10984

110-
private void saveMediaToFile()
111-
{
112-
new Thread(new Runnable(){
113-
public void run()
114-
{
115-
Uri uri = asset_intent.getData();
116-
try
117-
{
118-
AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r");
119-
if (file != null)
120-
{
121-
FileInputStream in = afd.createInputStream();
122-
FileOutputStream out = new FileOutputStream(file);
123-
byte[] buff = new byte[1024];
124-
int len;
125-
while ((len = in.read(buff)) > 0)
126-
{
127-
out.write(buff, 0, len);
128-
}
129-
in.close();
130-
out.flush();
131-
out.close();
132-
}
133-
}
134-
catch (Exception e)
135-
{
136-
e.printStackTrace();
137-
}
138-
process.sendEmptyMessage(MSG);
139-
}
140-
}).start();
141-
}
85+
private void saveMediaToFile() {
86+
new Thread(new Runnable() {
87+
public void run() {
88+
Uri uri = asset_intent.getData();
89+
try {
90+
AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r");
91+
if (file != null && afd != null) {
92+
FileInputStream in = afd.createInputStream();
93+
FileOutputStream out = new FileOutputStream(file);
94+
byte[] buff = new byte[1024];
95+
int len;
96+
while ((len = in.read(buff)) > 0) {
97+
out.write(buff, 0, len);
98+
}
99+
in.close();
100+
out.flush();
101+
out.close();
102+
}
103+
} catch (Exception e) {
104+
e.printStackTrace();
105+
}
106+
load.cancel();
107+
}
108+
}).start();
109+
}
142110

143111
}

0 commit comments

Comments
 (0)