【问题标题】:remove "Select image from gallery" option when app starts. Android应用程序启动时删除“从图库中选择图像”选项。安卓
【发布时间】:2013-07-03 13:11:30
【问题描述】:

我是 android 的新手。我尝试使用一些开源代码开发应用程序。 该应用程序使用图库中的图像,然后将其导入到 imageview 中,我们对图像进行一些操作。

我需要的是,应用程序应该从已经进入 imageview 的一些默认图像开始。然后我将添加一些按钮来稍后从图库中选择图像。我无法理解这里的工作流程。如果有人能指导我,那就太好了。

这是我的代码主要活动。其中还包含启动屏幕。

public class Main extends Activity {

static final String PREFS_FILE = "image_edit";

/** The URI of the Image to display. */


private int _wait;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main_screen);
    _wait = 1000;

    imageUri = null;
}

@Override
protected void onResume() {
    super.onResume();
    if (_wait != 0) {

        new CountDownTimer(_wait, _wait) {
            @Override
            public void onFinish() {
                if (imageUri != null) {
                    Intent viewActivity = new Intent(Main.this, Viewer.class);
                    viewActivity.putExtra("image", imageUri);
                    startActivity(viewActivity);
                } else {

                     startActivityForResult(newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),0);
                }
            }

            @Override
            public void onTick(long millisUntilFinished) {

            }

        }.start();
        _wait = 0;
    } else {
        if (imageUri != null) {
            Intent viewActivity = new Intent(this, Viewer.class);
            viewActivity.putExtra("image", imageUri);
            startActivity(viewActivity);
        } else {

             startActivityForResult(newIntent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 0);
        }
    }
}

@Override
protected void onPause() {
    super.onPause();
    imageUri = null;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        imageUri = data.getData();
    } else {
        System.exit(0);
        Log.e("result", "BAD");
      }
   }
}

然后从这里拍摄的图像转到下一个名为 Viewer 活动的活动,其 onresume 是这个

protected void onResume() {
    super.onResume();

    mHandler = new Handler();

    mPreferences = this.getSharedPreferences(Main.PREFS_FILE, 0);
    setContentView(R.layout.nothing);

    // Inflate all the views.
    int orientation = getResources().getConfiguration().orientation;
    basicInit(orientation);
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        initPortrait();

    } else {
        initLandscape();
    }

    Intent intent = getIntent();
    Uri imageURI = null;
    Log.e("URI:", intent.getData() + "");
    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SEND)) {
        Bundle extras = intent.getExtras();
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            imageURI = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
        }
    } else if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW)) {
        imageURI = intent.getData();
    } else {
        imageURI = (Uri) intent.getParcelableExtra("image");
    }

    addImage(imageURI);

    addDraggableImages();

    _updateImageTask = new UpdateImage(mRelative, mHandler);


   }

所以我尝试在主视图中执行此操作

 if(wait !=0){}
    else{
    startActivity(new Intent(Main.this, Viewer.class));
    }

但这不起作用..错误是runtimeexception

所以如果有人指导我会很有帮助..谢谢

【问题讨论】:

    标签: android image android-intent android-activity media


    【解决方案1】:

    正如您在 MainActivity 的 onResume() 方法中看到的那样,它首先会转到 else 部分,即 startActivityForResult,它将打开一个画廊并要求您选择一张图片。

    您可以简单地在 main_screen.xml 中创建一个按钮,而不是将其放入 onResume 中

    然后在该按钮的 onclickListener 中实现此功能。

    要默认查看图像,可以在 xml 的 Imageview 标签中输入:android:src="@drawable/imageFile"

    这对你有意义吗?

    【讨论】:

      【解决方案2】:

      下面给出了类似于你所说的应用程序的Java类和laoyout文件。

      MainActivity.java

            package com.tag.photocaptureandgallery;
      
          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.OutputStream;
      
          import android.app.Activity;
          import android.app.AlertDialog;
          import android.content.DialogInterface;
          import android.content.Intent;
          import android.database.Cursor;
          import android.graphics.Bitmap;
          import android.graphics.BitmapFactory;
          import android.net.Uri;
          import android.os.Bundle;
          import android.os.Environment;
          import android.provider.MediaStore;
          import android.provider.MediaStore.MediaColumns;
          import android.view.View;
          import android.view.View.OnClickListener;
          import android.widget.Button;
           import android.widget.ImageView;
      
           public class MainActivity extends Activity {
      
      private final int SELECT_FILE = 1;
      private final int REQUEST_CAMERA = 0;
      private ImageView ivImage;
      private Button btnSetImage;
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      
          ivImage = (ImageView) findViewById(R.id.ivImage);
          btnSetImage = (Button) findViewById(R.id.btnSelectPhoto);
      
          btnSetImage.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  selectImage();
              }
          });
      
      }
      
      private void selectImage() {
          final CharSequence[] items = { "Take Photo", "Choose from Library",
                  "Cancel" };
      
          AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
          builder.setTitle("Add Photo!");
          builder.setItems(items, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int item) {
                  if (items[item].equals("Take Photo")) {
                      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                      File f = new File(android.os.Environment
                              .getExternalStorageDirectory(), "temp.jpg");
                      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                      startActivityForResult(intent, REQUEST_CAMERA);
                  } else if (items[item].equals("Choose from Library")) {
                      Intent intent = new Intent(
                              Intent.ACTION_PICK,
                      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      intent.setType("image/*");
                      startActivityForResult(
                              Intent.createChooser(intent, "Select File"),
                              SELECT_FILE);
                  } else if (items[item].equals("Cancel")) {
                      dialog.dismiss();
                  }
              }
          });
          builder.show();
      }
      
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (resultCode == RESULT_OK) {
              if (requestCode == REQUEST_CAMERA) {
                  File f = new File(Environment.getExternalStorageDirectory()
                          .toString());
                  for (File temp : f.listFiles()) {
                      if (temp.getName().equals("temp.jpg")) {
                          f = temp;
                          break;
                      }
                  }
                  try {
                      Bitmap bm;
                      BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
      
                      bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                              btmapOptions);
      
                      // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                      ivImage.setImageBitmap(bm);
      
                      String path = android.os.Environment
                              .getExternalStorageDirectory()
                              + File.separator
                              + "Phoenix" + File.separator + "default";
                      f.delete();
                      OutputStream fOut = null;
                      File file = new File(path, String.valueOf(System
                              .currentTimeMillis()) + ".jpg");
                      try {
                          fOut = new FileOutputStream(file);
                          bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                          fOut.flush();
                          fOut.close();
                      } catch (FileNotFoundException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              } else if (requestCode == SELECT_FILE) {
                  Uri selectedImageUri = data.getData();
      
                  String tempPath = getPath(selectedImageUri, MainActivity.this);
                  Bitmap bm;
                  BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                  bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
                  ivImage.setImageBitmap(bm);
              }
          }
      }
      
      public String getPath(Uri uri, Activity activity) {
          String[] projection = { MediaColumns.DATA };
          Cursor cursor = activity
                  .managedQuery(uri, projection, null, null, null);
          int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);
      }
      

      }

      Main.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/LinearLayout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:padding="10dp" >
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:padding="5dp" >
      
          <Button
              android:id="@+id/btnSelectPhoto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Select Photo" />
      
      </LinearLayout>
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="10dp" >
      
          <ImageView
              android:id="@+id/ivImage"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:src="@drawable/ic_launcher" />
      
      </LinearLayout>
      
      </LinearLayout>
      

      同时添加使用相机所需的权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        相关资源
        最近更新 更多