2016-09-12 5 views
1

私はソーシャルメディア経由で画像を共有するAndroidアプリケーションを作っています。画像はグリッドレイアウトにあるので、ユーザーが画像を押すと、その画像を表示する別のアクティビティが開きます(これは今共有できます)。画像はドロワーブルフォルダーに保存されています。私が共有している画像を(より小さい - 32px)より大きい別のアクティビティで見るものよりも(172px)よりも欲しいです。あなたが店の前にそれをビットマップを拡大縮小、または第二の縮小画像を保存することができ大きな画像を表示しますが、小さい画像をAndroidで共有します

public class FullImage extends AppCompatActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.full_image); 

     Intent i = getIntent(); 

     int position = i.getExtras().getInt("id"); 
     ImageAdapter imageAdapter = new ImageAdapter(this); 

     ImageView imageView = (ImageView) findViewById(R.id.full_image_view); 
     imageView.setImageResource(imageAdapter.mThumbIds[position]); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     OutputStream output; 

     ImageView image = (ImageView) findViewById(R.id.full_image_view); 
     Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap(); 

     File dir = new File(Environment.getExternalStorageDirectory() + "/images"); 

     dir.mkdirs(); 

     File file = new File(dir, "images.png"); 

     try { 
      verifyStoragePermissions(this); 
      output = new FileOutputStream(file); 

      bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
      output.flush(); 
      output.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     switch (item.getItemId()) { 
      case R.id.action_share: 
       Uri uri = Uri.fromFile(file); 
       Intent shareIntent = new Intent(); 
       shareIntent.setAction(Intent.ACTION_SEND); 
       shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
       shareIntent.setType("image/png"); 
       startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share))); 

      case android.R.id.home: 
       onBackPressed(); 
       return true; 
     } 
     return true; 

    } 

    // Storage Permissions 
    private static final int REQUEST_EXTERNAL_STORAGE = 1; 
    private static String[] PERMISSIONS_STORAGE = { 
      Manifest.permission.READ_EXTERNAL_STORAGE, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE 
    }; 

    public static void verifyStoragePermissions(Activity activity) { 
     // Check if we have write permission 
     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

     if (permission != PackageManager.PERMISSION_GRANTED) { 
      // We don't have permission so prompt the user 
      ActivityCompat.requestPermissions(
        activity, 
        PERMISSIONS_STORAGE, 
        REQUEST_EXTERNAL_STORAGE 
      ); 
     } 
    } 
} 
+2

使用Bitmap.createScaledBitmapがオリジナルとは異なるサイズのビットマップを作成するには:https://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap、int型、int、boolean) –

+0

メイン画像からサムネイルを取り出すことができます。サイズは小さく、解像度は低くなります –

答えて

1

:ここ

はあなたに完全な画像を表示する活動です。

int PIXELS = 32; 
... 
bitmap = bitmap.createScaledBitmap(bitmap, PIXELS , PIXELS , true)); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
+0

ありがとう@adalPaRi ....チャームのように働いています –

関連する問題