Created
July 15, 2012 13:32
-
-
Save Nkzn/3116941 to your computer and use it in GitHub Desktop.
ファイルサイズ検証用コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
private static final int REQUEST_CODE_GALLERY = 0; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Button btnCallGallary = new Button(this); | |
btnCallGallary.setText("ギャラリーを開く"); | |
btnCallGallary.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); | |
intent.setType("image/*"); | |
startActivityForResult(intent, 0); | |
} | |
}); | |
setContentView(btnCallGallary); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK) { | |
Uri imageUri = data.getData(); | |
// ギャラリーのカーソルを作成 | |
ContentResolver contentResolver = getContentResolver(); | |
String[] filePathColumn = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; | |
Cursor cursor = contentResolver.query(imageUri, filePathColumn, null, null, null); | |
cursor.moveToFirst(); | |
// ファイルパスを取得 | |
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); | |
String filePath = cursor.getString(dataColumnIndex); | |
Toast.makeText(this, "filePath -> " + filePath + "\nfileSize -> " + new File(filePath).length(), Toast.LENGTH_LONG).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment