2017-04-01 6 views
0

Android Studio에서 이미지/동영상을 업로드하려고했는데 다른 Android 버전에서는 문제가 없었지만 누가와 관련된 문제가있었습니다.Android Studio에서 Nougat 버전의 이미지, 동영상 항목을 업로드 할 수 없습니다.

누구나 내가 이것을 누락시킬 수 있습니다.

package com.ethosapp.ethos.jobs;

import android.app.Notification; 
import android.app.NotificationManager; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.graphics.RectF; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Build; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v7.app.NotificationCompat; 
import android.util.Log; 

import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; 
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState; 
import com.amazonaws.services.s3.model.CannedAccessControlList; 
import com.birbit.android.jobqueue.Job; 
import com.birbit.android.jobqueue.Params; 
import com.birbit.android.jobqueue.RetryConstraint; 
import com.ethosapp.ethos.EthosApplication; 
import com.ethosapp.ethos.R; 
import com.ethosapp.ethos.models.Entry; 
import com.ethosapp.ethos.models.PostEntry; 
import com.ethosapp.ethos.services.AmazonSyncService; 
import com.ethosapp.ethos.services.ApiServiceFactory; 
import com.ethosapp.ethos.services.EntryService; 

import org.apache.commons.io.FilenameUtils; 
import org.apache.commons.io.IOUtils; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 

import retrofit2.Call; 

/** 
* Created by jaya on 25/05/16. 
*/ 
public class UploadFileToS3Job extends Job { 

    public static final int PRIORITY = 1; 

    protected String filename; 
    protected String uri; 
    protected String extension; 
    protected String fileId; 
    protected String entryId; 
    protected long type; 
    protected String state; 

    public UploadFileToS3Job(String filename, String uri, String extension, String fileId, String entryId, long type) { 
     super(new Params(PRIORITY).requireNetwork().persist()); 
     this.filename = filename; 
     this.uri = uri; 
     this.extension = extension; 
     this.fileId = fileId; 
     this.type = type; 
     this.entryId = entryId; 

     try { 
      Uri fileUri = Uri.parse(uri); 
      File f = new File(fileUri.getPath()); 
      JSONObject props = new JSONObject(); 
      props.put("entryId", this.entryId); 
      props.put("fileId", this.fileId); 
      props.put("extension", this.extension); 
      props.put("fileSize", f.length()); 
      EthosApplication.mixPanelTrack("BeginUploadFile", props); 
     } catch (JSONException jsonException) { 
      Log.e("MYAPP", "Unable to add properties to JSONObject", jsonException); 
     } 
    } 

    public UploadFileToS3Job(String filename) { 
     super(new Params(PRIORITY).requireNetwork().persist()); 
     Log.e("tag", "fileNAAM" + filename); 
     this.filename = filename; 
    } 




    @Override 
    public void onAdded() { 

    } 

    @Override 
    public void onRun() throws Throwable { 
     switch ((int) this.type) { 
      case Entry.Type.IMAGE: 
       uploadImageFiles(); 
       break; 
      case Entry.Type.AUDIO: 
       uploadVideoAudioFiles(); 
       break; 
      case Entry.Type.VIDEO: 
       uploadVideoAudioFiles(); 
       break; 
     } 
    } 

    @Override 
    protected void onCancel(int cancelReason) { 

    } 

    @Override 
    protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount, int maxRunCount) { 
     return null; 
    } 

    @Nullable 
    private File saveFileToTempFolder(Uri uri, String name, String extension) throws Throwable { 
     InputStream inputStream = EthosApplication.getInstance().getContentResolver().openInputStream(uri); 
     File outputDir = EthosApplication.getInstance().getBaseContext().getCacheDir(); // context being the Activity pointer 
     File tempFile = File.createTempFile(name, extension, outputDir); 
     FileOutputStream out = new FileOutputStream(tempFile); 
     IOUtils.copy(inputStream, out); 
     inputStream.close(); 
     out.close(); 
     return tempFile; 
    } 

    private void uploadVideoAudioFiles() throws Throwable { 

     Uri fileUri = Uri.parse(uri); 
     File file = new File(fileUri.getPath()); 
     Log.e("FFFFFFFFF====","=="+"content://com.ethosapp.ethos.provider/external_files/EthOS/EthOS%20movies/"+filename); 

     if (file.exists()) { 
      String parent = fileId + "/"; 
      uploadFileToS3(parent + filename, file, CannedAccessControlList.PublicRead); 

      // Now schedule a media-transcode 
      // Update the Entry 
      PostEntry postEntry = new PostEntry(); 
      postEntry.setFileId(fileId); 

      EntryService service = ApiServiceFactory.createRetrofitService(EntryService.class, ApiServiceFactory.API_BASE_URL); 

      Call<Entry> entryCall = service.scheduleMediaTranscode(this.entryId, postEntry); 
      Entry entry = entryCall.execute().body(); 

      PostEntry postEntry1 = new PostEntry(); 
      postEntry.setType("0"); 

      Call<Entry> entryCall1 = service.updateEntry(this.entryId, postEntry1); 
      Entry entry1 = entryCall1.execute().body(); 

      // TODO: Some Validation to verify the entry is uploaded successfully 

      Intent intent = new Intent(); 
      intent.setAction("com.ethosapp.ethos.CUSTOM_INTENT"); 
      EthosApplication.getInstance().getBaseContext().sendBroadcast(intent); 

     } else { 
      // TODO : Error Handling 
     } 
    } 

    @NonNull 
    private void uploadImageFiles() throws Throwable { 
     Log.e("tag", "images" + uri); 
     Uri fileUri = Uri.parse(uri); 
     Log.e("tag", "<----fileUri---->" + uri); 

     String fileNameWithOutExt = FilenameUtils.removeExtension(filename); 
     String fileExtension = FilenameUtils.getExtension(filename); 
     String parent = fileId + "/"; 

     // Upload preview image 
     // name : name-preview.jpg (230x0) 
     // Dimensions: 230 x 0 
     // Quality: 70 
     String previewImageFilename = fileNameWithOutExt + "-preview" + "." + fileExtension; 
     File previewImage = resizeImage(fileUri, 230, 230, previewImageFilename, 70); 
     uploadFileToS3(parent + previewImageFilename, previewImage, CannedAccessControlList.PublicRead); 
     previewImage.delete(); 

     // Upload resized image 
     // name : name-resized.jpg (230x0) 
     // Dimensions: 1024 x 768 
     // Quality: 70 
     String resizedImageFilename = fileNameWithOutExt + "-resized" + "." + fileExtension; 
     File resizedImage = resizeImage(fileUri, 1024, 768, resizedImageFilename, 70); 
     uploadFileToS3(parent + resizedImageFilename, resizedImage, CannedAccessControlList.PublicRead); 
     resizedImage.delete(); 


     // Upload resized image 
     // name : name-resized.jpg (230x0) 
     // Dimensions: 480 x 800 
     // Quality: 60 
     String resized1XImageFilename = fileNameWithOutExt + "-resized-1x" + "." + fileExtension; 
     File resized1XImage = resizeImage(fileUri, 480, 800, resized1XImageFilename, 60); 
     uploadFileToS3(parent + resizedImageFilename, resized1XImage, CannedAccessControlList.PublicRead); 
     resized1XImage.delete(); 

     // Upload resized image 
     // name : name-resized.jpg (230x0) 
     // Dimensions: 1024 x 768 
     // Quality: 60 
     String resized2XImageFilename = fileNameWithOutExt + "-resized-2x" + "." + fileExtension; 
     File resized2XImage = resizeImage(fileUri, 1024, 768, resized2XImageFilename, 60); 
     uploadFileToS3(parent + resizedImageFilename, resized2XImage, CannedAccessControlList.PublicRead); 
     resized2XImage.delete(); 

     // Upload thumbnail image 
     // name : name-tn.jpg (230x0) 
     // Dimensions: 80 x 80 
     // Quality: 70 
     String thumbnailImageFilename = fileNameWithOutExt + "-tn" + "." + fileExtension; 
     File thumbnailImage = resizeImage(fileUri, 80, 80, thumbnailImageFilename, 70); 
     uploadFileToS3(parent + thumbnailImageFilename, thumbnailImage, CannedAccessControlList.PublicRead); 
     thumbnailImage.delete(); 

     //File image = saveFileToTempFolder(fileUri, filename, extension); 
     File image = new File(fileUri.getPath()); 
     uploadFileToS3(parent + filename, image, CannedAccessControlList.Private); 

     // Update the Entry 
     PostEntry postEntry = new PostEntry(); 
     postEntry.setType("0"); 

     EntryService service = ApiServiceFactory.createRetrofitService(EntryService.class, ApiServiceFactory.API_BASE_URL); 

     Call<Entry> entryCall = service.updateEntry(this.entryId, postEntry); 
     Entry entry = entryCall.execute().body(); 

     int id=1; 

     Context baseContext = EthosApplication.getInstance().getBaseContext(); 

//  // intent triggered, you can add other intent for other actions 
//  Intent intent = new Intent(baseContext, ProjectEntriesActivity.class); 
//  PendingIntent pIntent = PendingIntent.getActivity(baseContext, 0, intent, 0); 

     NotificationManager mNotifyManager = 
       (NotificationManager) baseContext.getSystemService(Context.NOTIFICATION_SERVICE); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(baseContext); 
     mBuilder.setContentTitle("Entry Uploaded") 
       .setContentText("Entry has uploaded successfully.") 
       .setSmallIcon(R.drawable.ic_logo) 
       .setPriority(Notification.PRIORITY_HIGH); 

     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setSound(alarmSound); 

     mNotifyManager.notify(id, mBuilder.build()); 

     //Log.e("tag","1111"); 
     // TODO: Some Validation to verify the entry is uploaded successfully 
     //Intent intent = new Intent(); 
     //intent.setAction("com.ethosapp.ethos.CUSTOM_INTENT"); 
     //EthosApplication.getInstance().getBaseContext().sendBroadcast(intent); 

    } 


    private void uploadFileToS3(String filename, File file, CannedAccessControlList acl) throws Throwable { 

     Log.e("FILE=======","=="+file); 
     Log.e("FILE=======","=="+filename); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
     { 
      String ff="content://com.ethosapp.ethos.provider/external_files/EthOS/EthOS%20Pictures/"+filename; 
      Log.e("FFFF==","=="+ff); 
     } 
     String ff="content://com.ethosapp.ethos.provider/external_files/EthOS/EthOS%20Pictures/"+filename; 
     Log.e("FFFF==","=="+ff); 

     Log.e("1111","==================="); 
     AmazonSyncService syncService = new AmazonSyncService(EthosApplication.getInstance().getBaseContext()); 
     TransferObserver transferObserver = syncService.startUpload(filename, file, acl); 
     transferObserverListener(transferObserver); 

     // Code to make the thread to wait for upload completion. 
     // Entire code will be running in a background thread, so there is no 
     // issue in waiting for it to complete. 
     while (true) { 
      if (TransferState.COMPLETED.equals(transferObserver.getState()) 
        || TransferState.FAILED.equals(transferObserver.getState())) { 
       break; 
      } 
     } 

     if (TransferState.FAILED.equals(transferObserver.getState())) { 
      throw new Exception("Upload File to S3 failed."); 
     } 
    } 

    private void transferObserverListener(TransferObserver transferObserver) { 
     transferObserver.setTransferListener(new TransferListener() { 
      @Override 
      public void onStateChanged(int id, TransferState state) { 
       Log.e("statechange", state + ""); 

       try { 
        Uri fileUri = Uri.parse(uri); 
        File f = new File(fileUri.getPath()); 
        JSONObject props = new JSONObject(); 
        props.put("entryId", entryId); 
        props.put("fileId", fileId); 
        props.put("extension", extension); 
        props.put("fileSize", f.length()); 
        EthosApplication.mixPanelTrack("UploadedFile", props); 
       } catch (JSONException jsonException) { 
        Log.e("MYAPP", "Unable to add properties to JSONObject", jsonException); 
       } 
      } 

      @Override 
      public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { 
       int percentage = (int) (bytesCurrent/bytesTotal * 100); 
       Log.e("percentage", percentage + ""); 

       if(type == Entry.Type.VIDEO){ 
        try { 
         Uri fileUri = Uri.parse(uri); 
         File f = new File(fileUri.getPath()); 
         JSONObject props = new JSONObject(); 
         props.put("entryId", entryId); 
         props.put("fileId", fileId); 
         props.put("extension", extension); 
         props.put("fileSize", f.length()); 
         props.put("percentage", percentage); 
         EthosApplication.mixPanelTrack("UploadingFile", props); 
        } catch (JSONException jsonException) { 
         Log.e("MYAPP", "Unable to add properties to JSONObject", jsonException); 
        } 
       } 
      } 

      @Override 
      public void onError(int id, Exception ex) { 
       Log.e("error", "error"); 
      } 
     }); 
    } 

    private File resizeImage(Uri fileUri, int width, int height, String resizedFilename, int quality) throws Throwable { 
     int inWidth = 0; 
     int inHeight = 0; 

     InputStream inputStream = EthosApplication.getInstance().getContentResolver().openInputStream(fileUri); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     Bitmap bitmapOrg = BitmapFactory.decodeStream(inputStream, null, options); 
     inputStream.close(); 

     // save width and height 
     inWidth = options.outWidth; 
     inHeight = options.outHeight; 

     inputStream = EthosApplication.getInstance().getContentResolver().openInputStream(fileUri); 
     options = new BitmapFactory.Options(); 
     // calc rough re-size (this is no exact resize) 
     options.inSampleSize = Math.max(inWidth/width, inHeight/height); 
     // decode full image 
     Bitmap roughBitmap = BitmapFactory.decodeStream(inputStream, null, options); 
     // calc exact destination size 
     Matrix m = new Matrix(); 
     RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); 
     RectF outRect = new RectF(0, 0, width, height); 
     m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); 
     float[] values = new float[9]; 
     m.getValues(values); 

     // resize bitmap 
     Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); 
     File outputDir = EthosApplication.getInstance().getBaseContext().getCacheDir(); // context being the Activity pointer 
     File tempFile = File.createTempFile(resizedFilename, "jpg", outputDir); 
     Log.e("outputDir=====","=="+outputDir); 
     Log.e("tempFile=====","=="+tempFile); 
     FileOutputStream out = new FileOutputStream(tempFile); 
     resizedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out); 

     return tempFile; 
    } 



} 



public class AmazonSyncService { 


    // Initialize the Amazon Cognito credentials provider 
    public static CognitoCachingCredentialsProvider credentialsProvider; 

    // Create an S3 client 
    private AmazonS3 s3; 

    private TransferUtility transferUtility; 
    private Context context; 


    private final String MY_APP_BUCKET = "ethosappcom-production-media"; 
    private final String MY_APP_BUCKET_PROFILE = "ethosappcom-production-user"; 

    public AmazonSyncService(Context context){ 
     this.context = context; 
     credentialsProvider = new CognitoCachingCredentialsProvider(
       this.context.getApplicationContext(), 
       "us-east-1:e1922be7-9b7a-421e-bf3f-ae41b0a61315", // Identity Pool ID 
       Regions.US_EAST_1 // Region 
     ); 

     s3 = new AmazonS3Client(credentialsProvider); 
     s3.setRegion(Region.getRegion(Regions.US_EAST_1)); 

     transferUtility = new TransferUtility(s3, this.context.getApplicationContext()); 
    } 

    public CognitoCachingCredentialsProvider getCredentialProvider(){ 
     return credentialsProvider; 
    } 

    public TransferObserver startUpload(String filename, File file, CannedAccessControlList acl){ 
     return transferUtility.upload(MY_APP_BUCKET, filename, file, acl); 
    } 


} 
+0

'누우와 직면 한 문제'로 문제를 지정할 수도 있습니다! –

+0

@android_griezmann - 나는 ASW webservice를 백엔드에 사용했고 매개 변수 값을 추가 할 수 있지만 이미지 데이터를 업로드하려고 시도 할 때 이미지 데이터를 nouget에있는 서버로 전달하지 않습니다. 이걸 고칠 수있게 도와 주실 래요 ??? –

+0

이미지 업로드 방법에 대한 코드 스 니펫? 필요합니다. –

답변

2

문제는이 라인에

Uri fileUri = Uri.parse(uri); 

이 문제는 안드로이드 누가 변경된다 (여기서 - 이제까지는 기록 된 것). Nougat과 동일하게하려면 을 참조하십시오. this thread을 참조하십시오. 그것은 파일 제공자을 targetSDK를 24로 설정하고 다음을 변경할 때 사용하는 방법을 보여줍니다. 당신의 private static Uri getImageFileUri() 방법

Uri fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile()); 

호프

변경이 줄을

Uri fileUri = Uri.parse(uri); 

이것은 당신이 당신의 문제를 해결하는 데 도움이 될 것입니다.
더 많은 정보 - Setting Up File Sharing - Offical documentation