Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.andyshon.teachmeclient_my;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.provider.MediaStore;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.microsoft.azure.storage.CloudStorageAccount;
- import com.microsoft.azure.storage.blob.CloudBlobClient;
- import com.microsoft.azure.storage.blob.CloudBlobContainer;
- import com.microsoft.azure.storage.blob.ListBlobItem;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- public class BlobManagerActivity extends AppCompatActivity {
- private int PICK_IMAGE_REQUEST = 1;
- private Uri imageUri;
- private String imageName = "";
- public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"
- + "AccountName=teachme;"
- + "AccountKey=00Fbb3fwzAZ0PRj5h3ilt+kWen/vosQuNQKauzau/UsIn92Ct+x5S/zCc8zsvPxM9HbSxeXmAbSdN6QNhMo2IA==";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_blob_manager);
- try
- {
- // Setup the cloud storage account.
- CloudStorageAccount account = CloudStorageAccount
- .parse(BlobManagerActivity.storageConnectionString);
- // Create a blob service client
- CloudBlobClient blobClient = account.createCloudBlobClient();
- // Get a reference to a images container
- CloudBlobContainer container = blobClient.getContainerReference("images");
- // Loop over blobs within the container and output the URI to each of them.
- System.out.println("Список всех загруженных изображений");
- for (ListBlobItem blobItem : container.listBlobs()) {
- System.out.println("image = " + blobItem.getUri());
- }
- }
- catch (Exception e)
- {
- // Output the stack trace.
- e.printStackTrace();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
- imageUri = data.getData();
- try {
- Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
- // Log.d(TAG, String.valueOf(bitmap));
- ImageView imageView = (ImageView) findViewById(R.id.imageView);
- imageView.setImageBitmap(bitmap);
- System.out.println("image URI:" + imageUri);
- String imgName = getRealPathFromURI(imageUri);
- System.out.println("getRealPathFromURI = " + imgName);
- String str = imgName.substring(imgName.lastIndexOf("/")+1);
- System.out.println("new str = " + str);
- imageName = str;
- int random_number = 100000 + (int) (Math.random() * 999999);
- imageName = String.valueOf(random_number) + "_" + imageName;
- System.out.println("new uniq image name = " + imageName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // And to convert the image URI to the direct file system path of the image file
- public String getRealPathFromURI(Uri contentUri) {
- // can post image
- String [] proj={MediaStore.Images.Media.DATA};
- Cursor cursor = managedQuery( contentUri,
- proj, // Which columns to return
- null, // WHERE clause; which rows to return (all rows)
- null, // WHERE clause selection arguments (none)
- null); // Order-by clause (ascending by name)
- int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- return cursor.getString(column_index);
- }
- public void ChooseImageClick(View view) {
- Intent intent = new Intent();
- // Show only images, no videos or anything else
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- // Always show the chooser (if there are multiple options available)
- startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
- }
- public void UploadResourceClick(View view) {
- try {
- final InputStream inputStream = getContentResolver().openInputStream(imageUri);
- final int imageLength = inputStream.available();
- /*new BlobUploadTask(this, (TextView) findViewById(R.id.textView), inputStream, imageLength, "testImg01.jpg")
- .execute();*/
- new BlobUploadTask(this, (TextView) findViewById(R.id.textView), inputStream, imageLength, imageName)
- .execute();
- } catch (final Throwable t) {
- printException(t);
- }
- }
- /**
- * Prints the specified text value to the view and to LogCat.
- *
- * @param view The view to print to.
- * @param value The value to print.
- */
- public void outputText(final TextView view, final String value) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- view.append(value + "\n");
- System.out.println(view);
- }
- });
- }
- /**
- * Clears the text from the specified view.
- *
- * @param view The view to clear.
- */
- public void clearText(final TextView view) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- view.setText("");
- }
- });
- }
- /**
- * Prints out the exception information .
- */
- public void printException(Throwable t) {
- StringWriter stringWriter = new StringWriter();
- PrintWriter printWriter = new PrintWriter(stringWriter);
- t.printStackTrace(printWriter);
- outputText(
- (TextView) findViewById(R.id.textView),
- String.format(
- "Got an exception from running samples. Exception details:\n%s\n",
- stringWriter.toString()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement