Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ファイルサイズ検証用コード
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
You can’t perform that action at this time.