Advertisement
andyshon

BlobManagerActivity.java

Jan 5th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. package com.andyshon.teachmeclient_my;
  2.  
  3.  
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.graphics.Bitmap;
  7. import android.net.Uri;
  8. import android.provider.MediaStore;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.microsoft.azure.storage.CloudStorageAccount;
  17. import com.microsoft.azure.storage.blob.CloudBlobClient;
  18. import com.microsoft.azure.storage.blob.CloudBlobContainer;
  19. import com.microsoft.azure.storage.blob.ListBlobItem;
  20.  
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.PrintWriter;
  24. import java.io.StringWriter;
  25.  
  26.  
  27. public class BlobManagerActivity extends AppCompatActivity {
  28.  
  29.     private int PICK_IMAGE_REQUEST = 1;
  30.     private Uri imageUri;
  31.     private String imageName = "";
  32.  
  33.     public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"
  34.             + "AccountName=teachme;"
  35.             + "AccountKey=00Fbb3fwzAZ0PRj5h3ilt+kWen/vosQuNQKauzau/UsIn92Ct+x5S/zCc8zsvPxM9HbSxeXmAbSdN6QNhMo2IA==";
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_blob_manager);
  41.  
  42.  
  43.         try
  44.         {
  45.             // Setup the cloud storage account.
  46.             CloudStorageAccount account = CloudStorageAccount
  47.                     .parse(BlobManagerActivity.storageConnectionString);
  48.  
  49.             // Create a blob service client
  50.             CloudBlobClient blobClient = account.createCloudBlobClient();
  51.  
  52.             // Get a reference to a images container
  53.             CloudBlobContainer container = blobClient.getContainerReference("images");
  54.  
  55.             // Loop over blobs within the container and output the URI to each of them.
  56.             System.out.println("Список всех загруженных изображений");
  57.             for (ListBlobItem blobItem : container.listBlobs()) {
  58.                 System.out.println("image = " + blobItem.getUri());
  59.             }
  60.         }
  61.         catch (Exception e)
  62.         {
  63.             // Output the stack trace.
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  70.         super.onActivityResult(requestCode, resultCode, data);
  71.  
  72.         if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
  73.  
  74.             imageUri = data.getData();
  75.  
  76.             try {
  77.                 Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
  78.  
  79.                 // Log.d(TAG, String.valueOf(bitmap));
  80.  
  81.                 ImageView imageView = (ImageView) findViewById(R.id.imageView);
  82.                 imageView.setImageBitmap(bitmap);
  83.                 System.out.println("image URI:" + imageUri);
  84.                 String imgName = getRealPathFromURI(imageUri);
  85.                 System.out.println("getRealPathFromURI = " + imgName);
  86.                 String str = imgName.substring(imgName.lastIndexOf("/")+1);
  87.                 System.out.println("new str = " + str);
  88.                 imageName = str;
  89.                 int random_number = 100000 + (int) (Math.random() * 999999);
  90.                 imageName = String.valueOf(random_number) + "_" + imageName;
  91.                 System.out.println("new uniq image name = " + imageName);
  92.             } catch (IOException e) {
  93.                 e.printStackTrace();
  94.             }
  95.         }
  96.     }
  97.  
  98.     // And to convert the image URI to the direct file system path of the image file
  99.     public String getRealPathFromURI(Uri contentUri) {
  100.  
  101.         // can post image
  102.         String [] proj={MediaStore.Images.Media.DATA};
  103.         Cursor cursor = managedQuery( contentUri,
  104.                 proj, // Which columns to return
  105.                 null,       // WHERE clause; which rows to return (all rows)
  106.                 null,       // WHERE clause selection arguments (none)
  107.                 null); // Order-by clause (ascending by name)
  108.         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  109.         cursor.moveToFirst();
  110.  
  111.         return cursor.getString(column_index);
  112.     }
  113.  
  114.     public void ChooseImageClick(View view) {
  115.         Intent intent = new Intent();
  116.  
  117.         // Show only images, no videos or anything else
  118.         intent.setType("image/*");
  119.         intent.setAction(Intent.ACTION_GET_CONTENT);
  120.  
  121.         // Always show the chooser (if there are multiple options available)
  122.         startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
  123.     }
  124.  
  125.     public void UploadResourceClick(View view) {
  126.         try {
  127.             final InputStream inputStream = getContentResolver().openInputStream(imageUri);
  128.             final int imageLength = inputStream.available();
  129.  
  130.             /*new BlobUploadTask(this, (TextView) findViewById(R.id.textView), inputStream, imageLength, "testImg01.jpg")
  131.                     .execute();*/
  132.             new BlobUploadTask(this, (TextView) findViewById(R.id.textView), inputStream, imageLength, imageName)
  133.                     .execute();
  134.         } catch (final Throwable t) {
  135.             printException(t);
  136.         }
  137.  
  138.     }
  139.  
  140.     /**
  141.      * Prints the specified text value to the view and to LogCat.
  142.      *
  143.      * @param view  The view to print to.
  144.      * @param value The value to print.
  145.      */
  146.     public void outputText(final TextView view, final String value) {
  147.         runOnUiThread(new Runnable() {
  148.             @Override
  149.             public void run() {
  150.                 view.append(value + "\n");
  151.                 System.out.println(view);
  152.             }
  153.         });
  154.     }
  155.  
  156.     /**
  157.      * Clears the text from the specified view.
  158.      *
  159.      * @param view The view to clear.
  160.      */
  161.     public void clearText(final TextView view) {
  162.         runOnUiThread(new Runnable() {
  163.             @Override
  164.             public void run() {
  165.                 view.setText("");
  166.             }
  167.         });
  168.     }
  169.  
  170.     /**
  171.      * Prints out the exception information .
  172.      */
  173.     public void printException(Throwable t) {
  174.         StringWriter stringWriter = new StringWriter();
  175.         PrintWriter printWriter = new PrintWriter(stringWriter);
  176.         t.printStackTrace(printWriter);
  177.         outputText(
  178.                 (TextView) findViewById(R.id.textView),
  179.                 String.format(
  180.                         "Got an exception from running samples. Exception details:\n%s\n",
  181.                         stringWriter.toString()));
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement