Skip to content

Commit

Permalink
Andorid 7.x でFileUriExposedExceptionがでる問題に対処
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Mar 15, 2017
1 parent c4a0118 commit 8483d8f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "jp.juggler.fadownloader"
minSdkVersion 14
targetSdkVersion 25
versionCode 10
versionName "1.6"
versionCode 11
versionName "1.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
Expand All @@ -15,7 +17,9 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.BaseColumns;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
Expand Down Expand Up @@ -245,25 +249,25 @@ public void bind(

rv = exif.getTagRationalValue( ExifInterface.TAG_FOCAL_LENGTH );
if( rv != null ){
sv = String.format( "%.1f", rv.toDouble() ).replaceAll("\\.0*$","");
exif_list.add(sv+"mm" );
sv = String.format( "%.1f", rv.toDouble() ).replaceAll( "\\.0*$", "" );
exif_list.add( sv + "mm" );
}

rv = exif.getTagRationalValue( ExifInterface.TAG_F_NUMBER );
if( rv != null ){
sv = String.format( "%.1f", rv.toDouble() ).replaceAll("\\.0*$","");
exif_list.add("F"+sv );
sv = String.format( "%.1f", rv.toDouble() ).replaceAll( "\\.0*$", "" );
exif_list.add( "F" + sv );
}

rv = exif.getTagRationalValue( ExifInterface.TAG_EXPOSURE_TIME );
if( rv != null ){
double dv = rv.toDouble();
if( dv > 0.25d ){
sv = String.format( "%.1f", dv ).replaceAll("\\.0*$","");
exif_list.add( sv+"s");
sv = String.format( "%.1f", dv ).replaceAll( "\\.0*$", "" );
exif_list.add( sv + "s" );
}else{
sv = String.format( "%.1f", 1/dv ).replaceAll("\\.0*$","");
exif_list.add( "1/"+sv+"s");
sv = String.format( "%.1f", 1 / dv ).replaceAll( "\\.0*$", "" );
exif_list.add( "1/" + sv + "s" );
}
}

Expand Down Expand Up @@ -643,6 +647,7 @@ void action_view( DownloadRecord data ){
tmp_info = fixFileURL( data );
}
if( tmp_info == null ) return;
registMediaURI( tmp_info );

Intent intent = new Intent( Intent.ACTION_VIEW );
if( tmp_info.mime_type != null ){
Expand All @@ -666,6 +671,7 @@ void action_send( DownloadRecord data ){
tmp_info = fixFileURL( data );
}
if( tmp_info == null ) return;
registMediaURI( tmp_info );

Intent intent = new Intent( Intent.ACTION_SEND );
if( tmp_info.mime_type != null ){
Expand All @@ -679,4 +685,40 @@ void action_send( DownloadRecord data ){
}
}

private void registMediaURI( Utils.FileInfo tmp_info ){
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ){
if( ! "file".equals( tmp_info.uri.getScheme() ) ) return;
String path = tmp_info.uri.getPath();
Uri files_uri = MediaStore.Files.getContentUri( "external" );
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(
files_uri
, null
, MediaStore.Files.FileColumns.DATA + "=?"
, new String[]{ path }
, null
);
if( cursor != null ){
try{
if( cursor.moveToFirst() ){
int colidx_id = cursor.getColumnIndex( BaseColumns._ID );
long id = cursor.getLong( colidx_id );
tmp_info.uri = Uri.parse( files_uri.toString() + "/" + id );
return;
}
}finally{
cursor.close();
}
}
ContentValues cv = new ContentValues( );
String name = new File(path).getName();
cv.put(MediaStore.Files.FileColumns.DATA,path);
cv.put(MediaStore.Files.FileColumns.DISPLAY_NAME,name);
cv.put(MediaStore.Files.FileColumns.TITLE,name);
cv.put(MediaStore.Files.FileColumns.MIME_TYPE,tmp_info.mime_type);
Uri new_uri = cr.insert(files_uri,cv);
if(new_uri !=null ) tmp_info.uri = new_uri;
}
}

}

0 comments on commit 8483d8f

Please sign in to comment.