画像のファイルヘッダを見てmimeTypeを修正する
This commit is contained in:
parent
4de2129585
commit
609b80d274
|
@ -29,7 +29,7 @@
|
|||
</value>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
@ -11,8 +11,8 @@ android {
|
|||
targetSdkVersion target_sdk_version
|
||||
minSdkVersion min_sdk_version
|
||||
|
||||
versionCode 285
|
||||
versionName "2.8.5"
|
||||
versionCode 286
|
||||
versionName "2.8.6"
|
||||
applicationId "jp.juggler.subwaytooter"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.Manifest
|
|||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.Dialog
|
||||
import android.content.ContentResolver
|
||||
import android.content.ContentValues
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
|
@ -101,6 +102,29 @@ class ActPost : AppCompatActivity(), View.OnClickListener, PostAttachment.Callba
|
|||
add("video/quicktime")
|
||||
}
|
||||
|
||||
private val imageHeaderList = arrayOf(
|
||||
Pair("image/jpeg",intArrayOf(0xff,0xd8,0xff,0xe0).toByteArray()),
|
||||
Pair("image/png",intArrayOf(0x89 ,0x50 ,0x4E ,0x47 ,0x0D ,0x0A ,0x1A ,0x0A).toByteArray()),
|
||||
Pair("image/gif", charArrayOf('G' ,'I' ,'F').toByteArray())
|
||||
)
|
||||
|
||||
private fun checkImageHeaderList(contentResolver:ContentResolver, uri : Uri) : String? {
|
||||
try{
|
||||
contentResolver.openInputStream(uri)?.use{ inStream ->
|
||||
val data = ByteArray(32)
|
||||
val nRead = inStream.read(data,0,data.size)
|
||||
for( pair in imageHeaderList ){
|
||||
val type = pair.first
|
||||
val header = pair.second
|
||||
if( nRead >= header.size && data.startWith(header) ) return type
|
||||
}
|
||||
}
|
||||
}catch(ex:Throwable){
|
||||
log.e(ex,"checkImageHeaderList failed.")
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// private void performCameraVideo(){
|
||||
//
|
||||
// try{
|
||||
|
@ -1481,8 +1505,17 @@ class ActPost : AppCompatActivity(), View.OnClickListener, PostAttachment.Callba
|
|||
|
||||
private fun getMimeType(uri : Uri, mimeTypeArg : String?) : String? {
|
||||
|
||||
// image/j()pg だの image/j(e)pg だの、mime type を誤記するアプリがあまりに多い
|
||||
// クレームで消耗するのを減らすためにファイルヘッダを確認する
|
||||
if(mimeTypeArg == null || mimeTypeArg.startsWith("image/")){
|
||||
val sv = checkImageHeaderList(contentResolver,uri)
|
||||
if( sv != null) return sv
|
||||
}
|
||||
|
||||
// 既に引数で与えられてる
|
||||
if(mimeTypeArg?.isNotEmpty() == true) return mimeTypeArg
|
||||
if(mimeTypeArg?.isNotEmpty() == true){
|
||||
return mimeTypeArg
|
||||
}
|
||||
|
||||
// ContentResolverに尋ねる
|
||||
var sv = contentResolver.getType(uri)
|
||||
|
@ -1495,6 +1528,8 @@ class ActPost : AppCompatActivity(), View.OnClickListener, PostAttachment.Callba
|
|||
return null
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private fun addAttachment(
|
||||
uri : Uri,
|
||||
|
|
|
@ -351,6 +351,40 @@ fun ByteArray.digestSHA256() : ByteArray {
|
|||
return digest.digest(this)
|
||||
}
|
||||
|
||||
fun ByteArray.startWith(
|
||||
key:ByteArray,
|
||||
thisOffset :Int =0,
|
||||
keyOffset:Int=0,
|
||||
length:Int = key.size-keyOffset
|
||||
):Boolean{
|
||||
if( this.size -thisOffset >= length && key.size -keyOffset >=length ){
|
||||
for( i in 0 until length ){
|
||||
if( this[i+thisOffset] != key[i+keyOffset]) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 各要素の下位8ビットを使ってバイト配列を作る
|
||||
fun IntArray.toByteArray():ByteArray{
|
||||
val dst = ByteArray(this.size)
|
||||
for(i in 0 until this.size){
|
||||
dst[i] = this[i].toByte()
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// 各要素の下位8ビットを使ってバイト配列を作る
|
||||
fun CharArray.toByteArray():ByteArray{
|
||||
val dst = ByteArray(this.size)
|
||||
for(i in 0 until this.size){
|
||||
dst[i] = this[i].toByte()
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
|
||||
//// MD5ハッシュの作成
|
||||
//@Suppress("unused")
|
||||
//fun String.digestMD5() : String {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
buildscript {
|
||||
|
||||
ext.kotlin_version = '1.2.61'
|
||||
ext.kotlin_version = '1.2.70'
|
||||
ext.kotlin_coroutines_version = '0.25.3'
|
||||
ext.anko_version='0.10.5'
|
||||
ext.asl_version='27.1.1'
|
||||
|
|
Loading…
Reference in New Issue